第一阶段代码输完了以后,运行,不能出现 百度的网页,显示空白,怎么回事啊
如题,代码
HttpThread.java
package com.jupiter.httpdemo; import android.os.Handler; import android.webkit.WebView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class HttpThread extends Thread{ private String url; private WebView webView; private Handler handler; public HttpThread(String url, WebView webView, Handler handler) { this.url = url; this.webView = webView; this.handler = handler; } @Override public void run() { try { URL httpsUrl = new URL(url); try { HttpsURLConnection conn = (HttpsURLConnection)httpsUrl.openConnection(); conn.setReadTimeout(5000); //请求超时时间 conn.setRequestMethod("GET"); //请求方式为 GET final StringBuffer sb = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; while ((str = reader.readLine())!= null){ sb.append(str); } handler.post(new Runnable() { @Override public void run() { webView.loadDataWithBaseURL(url, sb.toString(), "text/html", "uft-8", null); } }); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } } }
MainActivity.java
package com.jupiter.httpdemo; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; public class MainActivity extends AppCompatActivity { private String url ="http://www.baidu.com/"; private WebView wevView; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wevView = (WebView)findViewById(R.id.webView); new HttpThread("http://www.baidu.com",wevView,handler).start(); } }