為什么是空白的?
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統(tǒng)一定位符的一個(gè)縮寫
URL httpUrl=new URL(url);
//進(jìn)行網(wǎng)絡(luò)訪問:
try {
HttpURLConnection conn=(HttpURLConnection) httpUrl.openConnection();
? ?//設(shè)置請(qǐng)求超時(shí)的時(shí)間
conn.setReadTimeout(5000);
//設(shè)置網(wǎng)絡(luò)請(qǐng)求的方式:一般情況下通過Get訪問
conn.setRequestMethod("GET");
//拿到網(wǎng)頁(yè)回傳的信息StringBuffer(作為緩沖)
final StringBuffer sb=new StringBuffer();
//1.InputStreamReader副流轉(zhuǎn)換為字節(jié)流;通過getInputStream()拿到一個(gè)獨(dú)入流
BufferedReader readr=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
//如何拿到返回的信息:循環(huán)讀出流的數(shù)據(jù);
//1.readLine()表示讀一行,不等于空的情況下讓他繼續(xù)讀;
while((str=readr.readLine())!=null){
//通過sb.append填充數(shù)據(jù)
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>
2017-11-10
我也是空白
2017-03-13
我的也是╭∩╮(︶︿︶)╭∩╮鄙視你!(+﹏+)~狂暈
2016-12-11
我這里也是空白的,沒找到原因!