在我们之前学习Java的网络编程当中,我们已经见过HttpUrlConnection类的使用。HttpUrlConnect在Java是一个支持http特定的功能一个类,在许多的网络编程经常使用到它。今天我在这里记录的是android中使用HttpUrlConnection,之前我还学过使用异步加载来加载一个网站,它使用的原理同样是多线程,但是它使用的是用json来解析一个网站,而不是直接使用域名也就是网址来解析一个网站,而将要介绍的HttpUrlConnection是使用域名来解析一个网站的,当然它同时使用到了handler类,因为这个事例使用到了子线程来加载ui,主线程来更新ui。代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_http.MainActivity" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
从上面的xml中可以看出,我们只用了一个控件,那就是WebView。webview在加载网站当中自有它自己的妙处。
MainActivity类的代码:
package com.example.android_http;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webview = null;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webview);
new HttpThread("http://www.baidu.com", webview, handler).start();
}
}
其次是HttpThread类,这个继承类Thread类,创建了一个子线程来加载网页:
package com.example.android_http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import android.os.Handler;
import android.webkit.WebView;
public class HttpThread extends Thread{
private String url = null;
private WebView webview = null;
private Handler handler = null;
public HttpThread(String url, WebView webview, Handler handler)
{
this.handler = handler;
this.url = url;
this.webview = webview;
}
@Override
public void run() {
/*
* 关于HttpUrlConnection的使用,有什么不懂,请回去认真复习一下Java的网络编程
*/
HttpURLConnection httpurlconnection = null;
try {
URL url = new URL(this.url);
httpurlconnection = (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//允许超时5秒
try {
httpurlconnection.setReadTimeout(5000);
//以get的方式与服务器相连接
httpurlconnection.setRequestMethod("GET");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final StringBuffer sb = new StringBuffer();
String str = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream()));
try {
while((str = br.readLine()) != null)
{
sb.append(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
//第一个参数表示的是需要加载的数据--这个先前已经在输入流读入到了sb中
//第二参数表示的是需要加载网站的编码格式--百度的编码格式就是如此
/*
* 需要注意的是:
* loaddata不加载图片,如果想要加载图片,请使用loadDataWithBaseURL
*/
webview.loadData(sb.toString(), "text/html;charset=utf-8", null);
}
});
}
}