标签:image scroll inter 对象 效果 super android 使用 app
一、WebView
这个View就是一个浏览器,用于展示网页的。
布局文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 6 <WebView 7 android:id="@+id/web_view" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent"> 10 </WebView> 11 12 </LinearLayout>
java代码:
1 public class MainActivity extends AppCompatActivity{ 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 WebView webView = (WebView) findViewById(R.id.web_view); 8 webView.getSettings().setJavaScriptEnabled(true); 9 webView.setWebViewClient(new WebViewClient()); 10 webView.loadUrl("http://www.baidu.com"); 11 } 12 }
webView.getSettings().setJavaScriptEnabled(true)这个方法传入true,让WebView支持JavaScript脚本
webView.setWebViewClient(new WebViewClient()),实现了当需要从一个网页跳到另一个网页的时候,希望也在这个WebView中显示
AndroidManifest文件权限添加:
<uses-permission android:name="android.permission.INTERNET"/>
效果:
二、HTTP协议访问数据
工作原理:客户端向服务器发出一条HTTP请求,服务器收到请求后会返回一些数据给客户端,然后客户端再对数据进行解析和处理。
1、使用HttpURLConnection
基本用法:
* 首先获取到HttpURLConnection的实例
URL url = new URL("https://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
*设置HTTP请求是希望从服务器获取数据(GET),还是希望提交数服务器(POST)
connection.setRequestMethod("GET");
*设置连接超时的时间,读取超时的时间:
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
*调用getInputStream()方法获取到服务器的输入流:
InputStream in = connection.getInputStream();
*最后调用disconnect()把这个HTTP连接关掉:
connection.disconnect();
实例应用程序:
布局代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 6 <Button 7 android:id="@+id/send_request" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="发送" /> 11 12 <ScrollView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent"> 15 16 <TextView 17 android:id="@+id/response_text" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" /> 20 </ScrollView> 21 22 </LinearLayout>
java代码:
1 public class MainActivity extends AppCompatActivity{ 2 3 TextView responseText;//用于展示从服务器获取到的文本的控件 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 //控件注册 10 final Button sendRequest = (Button) findViewById(R.id.send_request); 11 responseText = (TextView) findViewById(R.id.response_text); 12 13 //按钮相应 14 sendRequest.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View view) { 17 sendRequestWithHttpURLConnection(); 18 } 19 }); 20 } 21 22 private void sendRequestWithHttpURLConnection(){ 23 24 //开启线程来发起网络请求 25 new Thread(new Runnable() { 26 @Override 27 public void run() { 28 HttpURLConnection connection = null; 29 BufferedReader reader = null; 30 try{ 31 URL url = new URL("https://www.baidu.com"); 32 connection = (HttpURLConnection) url.openConnection(); 33 connection.setRequestMethod("GET"); 34 connection.setConnectTimeout(8000); 35 connection.setReadTimeout(8000); 36 InputStream in = connection.getInputStream(); 37 38 //对获取到的输入流进行读取 39 reader = new BufferedReader(new InputStreamReader(in)); 40 StringBuilder response = new StringBuilder(); 41 String line; 42 while ((line = reader.readLine()) != null){ 43 response.append(line); 44 } 45 showResponse(response.toString()); 46 }catch (Exception e){ 47 e.printStackTrace(); 48 }finally { 49 if(reader != null){ 50 try{ 51 reader.close(); 52 }catch (IOException e){ 53 e.printStackTrace(); 54 } 55 } 56 if(connection != null){ 57 connection.disconnect(); 58 } 59 } 60 } 61 }).start(); 62 } 63 64 private void showResponse(final String response){ 65 runOnUiThread(new Runnable() { 66 @Override 67 public void run() { 68 //UI操作 69 responseText.setText(response); 70 } 71 }); 72 } 73 }
效果:
2、使用OkHttp
这是一个用来代替HttpURLConnection的开源库
基本用法:
*添加OkHttp库的依赖
compile ‘com.squareup.okhttp3:okhttp:3.9.0‘
*创建一个OkHttpClient的实例
OkHttpClient client = new OkHttpClient();
*创建一个Request实例,并连缀其他方法丰富这个对象
Request request = new Request.Builder() .url("http://www.baidu.com") .build();
*获取返回的数据
Response response = client.newCall(request).execute();
修改上面的程序:
1 public class MainActivity extends AppCompatActivity{ 2 3 TextView responseText;//用于展示从服务器获取到的文本的控件 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 //控件注册 10 final Button sendRequest = (Button) findViewById(R.id.send_request); 11 responseText = (TextView) findViewById(R.id.response_text); 12 13 //按钮相应 14 sendRequest.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View view) { 17 sendRequestWithHttpURLConnection(); 18 } 19 }); 20 } 21 22 private void sendRequestWithHttpURLConnection(){ 23 24 //开启线程来发起网络请求 25 new Thread(new Runnable() { 26 @Override 27 public void run() { 28 try{ 29 OkHttpClient client = new OkHttpClient(); 30 Request request = new Request.Builder() 31 .url("http://www.baidu.com") 32 .build(); 33 Response response = client.newCall(request).execute(); 34 String responseData = response.body().string(); 35 showResponse(responseData); 36 }catch (Exception e){ 37 e.printStackTrace(); 38 } 39 } 40 }).start(); 41 } 42 43 private void showResponse(final String response){ 44 runOnUiThread(new Runnable() { 45 @Override 46 public void run() { 47 //UI操作 48 responseText.setText(response); 49 } 50 }); 51 } 52 }
效果同样。。
标签:image scroll inter 对象 效果 super android 使用 app
原文地址:http://www.cnblogs.com/xxbbtt/p/7493212.html