为什么是空白的?
package com.example.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Handler;
import android.webkit.WebView;
public class HttpThread extends Thread{
private String url;
private WebView webview;
private Handler handler;
public HttpThread(String url,WebView webview,Handler hanlder){
this.url=url;
this.webview=webview;
this.handler=hanlder;
}
@Override
public void run() {
try {
//URL统一定位符的一个缩写
URL httpUrl=new URL(url);
//进行网络访问:
try {
HttpURLConnection conn=(HttpURLConnection) httpUrl.openConnection();
//设置请求超时的时间
conn.setReadTimeout(5000);
//设置网络请求的方式:一般情况下通过Get访问
conn.setRequestMethod("GET");
//拿到网页回传的信息StringBuffer(作为缓冲)
final StringBuffer sb=new StringBuffer();
//1.InputStreamReader副流转换为字节流;通过getInputStream()拿到一个独入流
BufferedReader readr=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
//如何拿到返回的信息:循环读出流的数据;
//1.readLine()表示读一行,不等于空的情况下让他继续读;
while((str=readr.readLine())!=null){
//通过sb.append填充数据
sb.append(str);
}
handler.post(new Runnable() {
@Override
public void run() {
webview.loadData(sb.toString(),"IE=edge,chrome=18", null);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.example.http;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
private WebView webview;
private Handler hanlder=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView) findViewById(R.id.webView1);
new HttpThread("http://www.hao123.com/?tn=90259734_hao_pg", webview, hanlder).start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.http"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>