[android] 网络html查看器
訪問一個網頁的請求實際上就是一個GET請求,應用的布局沒啥好說的,線性布局,定義好控件,在activity代碼里面先獲取到控件,獲取到EditText控件的網絡路徑,開啟get請求
?
開啟一個新的線程,new?Thread(){}.start()
獲取Url對象,new出來,參數:path是String的url,內部類訪問外部類的變量,應該頂一次final的
主線程中定義成員屬性Handler對象,為了方便直接重寫handleMessage()方法,回調過來的參數是Message對象,獲取Message對象的what屬性和obj屬性,
New出Url之后,會有異常產生,捕獲異常,
調用Url對象的openConnection()方法,得到HttpUrlConnection對象,這里需要強轉
調用HttpUrlConnection對象的setRequestMethod()方法
調用HttpUrlConnection對象的setConnectTimeout()方法
調用HttpUrlConnection對象的setRequestProperty()方法
調用HttpUrlConnection對象的getResponseCode()方法,得到響應碼,進行判斷
調用HttpUrlConnection對象的getInputStream()方法,得到InputStream對象
?
把流的數據轉換成文本,是一個非常常用的操作,新建一個包utils,放工具類
新建一個類StreamTools,里面定義一個靜態方法readInputStream()
獲取ByteArrayOutputStream對象,通過new一個字節數組輸出流
定義一個int的len長度是0
定義一個byte[]的數組,通過new?byte[1024]定義一個1024字節的數組
定義一個while循環,條件是調用InputStream對象的read(buffer)方法,參數:上面定義的byte[]數組,把數據讀入到byte[]數組里面,返回一個讀取的長度,如果長度等于-1那就是讀到末尾了,因此這個進行循環判斷
調用ByteArrayOutputStream對象的write(buffer,0,len)方法,讀取字節數組,從0開始到len長度
循環完成之后,關閉輸入流,調用ByteArrayOutputStream對象的toByteArray()得到一個字節數組,return出來用new?String()包裝一下
?
調用Handler對象的sendMessage()方法發送數據
?
當設置請求參數的時候,不能多加冒號,否則容易出錯
package com.tsh.hrmlviewer;import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;import com.tsh.hrmlviewer.utils.StreamTools;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends Activity {protected static final int SUCCESS = 1;protected static final int ERROR = 2;private EditText et_path;private TextView tv_show;// 消息處理器public Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case SUCCESS:String text = (String) msg.obj;tv_show.setText(text);break;case ERROR:Toast.makeText(MainActivity.this, "獲取數據失敗", 0).show();break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_path = (EditText) findViewById(R.id.et_path);tv_show = (TextView) findViewById(R.id.tv_show);}// 查看public void click(View v) {final String path = et_path.getText().toString().trim();if (TextUtils.isEmpty(path)) {Toast.makeText(this, "請輸入網址", 0).show();} else {// 開啟新線程new Thread() {public void run() {try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);int code = conn.getResponseCode();if (code == 200) {InputStream is = conn.getInputStream();String res = StreamTools.readInputStream(is);Message msg = new Message();msg.what = SUCCESS;msg.obj = res;handler.sendMessage(msg);} else {Message msg = new Message();msg.what = ERROR;handler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();Message msg = new Message();msg.what = ERROR;handler.sendMessage(msg);}}}.start();}}}工具類:
package com.tsh.hrmlviewer.utils;import java.io.ByteArrayOutputStream; import java.io.InputStream;public class StreamTools {/*** 讀取輸入流* @param is* @return*/public static String readInputStream(InputStream is){ByteArrayOutputStream baos=new ByteArrayOutputStream();int len=0;byte[] buffer=new byte[1024];try {while((len=is.read(buffer))!=-1){baos.write(buffer,0,len);}is.close();byte[] res=baos.toByteArray();return new String(res);} catch (Exception e) {e.printStackTrace();}return null;} }?
轉載于:https://www.cnblogs.com/taoshihan/p/5289742.html
總結
以上是生活随笔為你收集整理的[android] 网络html查看器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实现文件下载的方法总结
- 下一篇: XML-RPC远程方法调用