WebView控件的用法(主要用于显示网页)
首先申请网络权限:
<uses-permission android:name="android.permission.INTERNET" />
HttpURLConnection的使用中也要申请权限。
在布局中定义控件:
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
使用WebView打开百度网页:
public class WebViewTest extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_test);
WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.baidu.com");
}
}
WebView的getSetting() 方法可以去设置一些浏览器的属性,此处只是调用了setJavaScriptEnabled() 方法来 让WebView支持JavaScript脚本。
WebView的setWebViewClient() 方法,此方法接收一个WebViewClient实例,该方法可以保证当网页要跳转时仍然在WebView控件中显示。
最后调用WebView的loadUrl() 方法,并将网址传入,即可展示相应网页的内容。
使用HttpURLConnection
1.首先获取HttpURLConnection实例,一般只需new出一个URL对象并传入目标网络地址,然后调用openConnection() 方法即可:
URL url = new URL("http://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
2.然后调用setRequestMethod() 方法设置HTTP请求所使用的方法,常用的有GET和POST:
connection.setRequestMethod("GET");
3.接着进行一些自由定制,比如连接超时、读取超时的毫秒数等:
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
4.最后调用getInputStream() 方法 获取服务器返回的输入流:
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
5.别忘了调用disconnection() 方法将这个 HTTP连接关闭:
showResponse(content.toString());
简单实现访问百度网页:
public class HttpURLConnectionTest extends AppCompatActivity {
TextView responseView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_urlconnection_test);
responseView = (TextView) findViewById(R.id.response_text);
Button button = (Button) findViewById(R.id.http_url_connection);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder content = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
content.append(line);
}
showResponse(content.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
});
}
private void showResponse(String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
responseView.setText(response);
}
});
}
}
Android不允许在子线程中进行UI操作,所以此处采用runOnUiThread()方法将子线程切换为主线程。
|