html查看器android
阿新 • • 發佈:2017-06-30
html
1.android的API提供了訪問網絡的一個類HttpURLConnection
2.通過發送GET請求獲取服務器返回的html代碼
3.先看看布局文件,如下所示,
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取源碼"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#00f" android:hint="源碼顯示"/> </ScrollView></LinearLayout>
布局樣子:
4.下來是清單文件,記得連接網絡要添加權限
5.1再看java代碼,MainActivity的
package com.market.source;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.TextView;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.net.URLConnection;import butterknife.BindView;import butterknife.ButterKnife;public class MainActivity extends Activity { @BindView(R.id.et) EditText et; @BindView(R.id.tv) TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } public void get(View vew) throws IOException { final String str = et.getText().toString().trim(); new Thread(){ @Override public void run() { //1.請求地址url URL url = null; try { url = new URL(str); //2.獲取對這個地址的連接 HttpURLConnection con = (HttpURLConnection) url.openConnection(); //3.設置對這個地址的請求,GET請求 con.setRequestMethod("GET"); //4.設置請求參數 con.setConnectTimeout(5000); //5.獲取服務器的響應 int code = con.getResponseCode(); //6.根據響應嗎,判斷請求成功還是失敗,200成功 if(code==200){ //7.成功的話,服務器一流的形式返回數據 InputStream inputStream = con.getInputStream(); //8.這個流是字節流,需要我們轉換為字符流才可以認識 final String info = StreamTool.StreamtoString(inputStream); Log.e("MainActivity",info); runOnUiThread(new Runnable() { @Override public void run() { tv.setText(info); } }); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
5.2工具類,用來將一個流轉化為字符串
public class StreamTool { public static String StreamtoString(InputStream inputStream) { int len = -1; byte[] buffer = new byte[1024]; //內存數組輸出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { while((len=inputStream.read(buffer)) != -1){ baos.write(buffer,0,len); } String str = new String(baos.toByteArray()); return str; } catch (IOException e) { e.printStackTrace(); }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
6.運行效果查看
html查看器android