Android下載網頁源碼顯示沒圖片的網頁

運行效果:html

有圖片的網頁顯示不出來。java

XMLandroid

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

javaweb

public class web extends Activity {

	private WebView webView;
	StringBuffer data = new StringBuffer();

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web);
		// webView經過數據顯示網頁的組件
		webView = (WebView) findViewById(R.id.webView);
		// WebSettings做用:對WebView進行配置和管理
		WebSettings webSettings = webView.getSettings();
		webSettings.setLoadsImagesAutomatically(true); // 支持自動加載圖片
		webSettings.setUseWideViewPort(true); // 將圖片調整到適合webview的大小
		webSettings.setLoadWithOverviewMode(true); // 縮放至屏幕的大小
		// 若是訪問的頁面中要與Javascript交互,則webview必須設置支持Javascript
		webSettings.setJavaScriptEnabled(true);
		new webData().execute("https://www.baidu.com/");

	}

	class webData extends AsyncTask<String, Void, StringBuffer> {
		@Override
		protected void onPostExecute(StringBuffer result) {// 和Asynck參數三同樣
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			webView.loadData(data.toString(), "text/html;charset=utf-8",
					"utf-8");
		}

		@Override
		protected StringBuffer doInBackground(String... arg0) {// 和AsyncTask參數一同樣
			// TODO Auto-generated method stub
			URL url;
			try {
				url = new URL(arg0[0]);
				try {
					HttpURLConnection httpConection = (HttpURLConnection) url
							.openConnection();
					httpConection.setReadTimeout(60000);
					httpConection.setRequestMethod("GET");
					// 讀取數據部分
					InputStream inputStream = httpConection.getInputStream();// 此方法是鏈接返回的輸入流,是中轉站
					InputStreamReader inputReader = new InputStreamReader(
							inputStream, "utf-8");
					// inputStreamReader是字節流和字符流的中轉站,能夠轉換成bufferReader
					BufferedReader bufferReader = new BufferedReader(
							inputReader);
					String s;
					while ((s = bufferReader.readLine()) != null) {
						data.append(s);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return data;
		}
	}
}
相關文章
相關標籤/搜索