网页源代码查看器
這篇博文適用于剛剛接觸移動開發利用網絡連接加載數據的小伙伴們,
分廢話不多說,直接貼代碼。
MainActivity.class
package com.example.htmlcode;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.app.Activity;public class MainActivity extends Activity implements OnClickListener {private EditText edt_path;private Button btn_ok;private TextView tv_html;private static final int success=0; private static final int Error=1;private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch(msg.what){case success:tv_html.setText((String) msg.obj);break;case Error:Toast.makeText(MainActivity.this, "訪問失敗", 0).show();break;default:break;}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);edt_path=(EditText) findViewById(R.id.edt_URL);btn_ok=(Button) findViewById(R.id.btn_ok);tv_html=(TextView) findViewById(R.id.tv_html);btn_ok.setOnClickListener(this);}@Overridepublic void onClick(View v) {final String path=edt_path.getText().toString();//訪問網絡時new一個子線程new Thread(new Runnable() {@Overridepublic void run() {//請求網絡String html= getHtmlFromNet(path);if(!TextUtils.isEmpty(html)){//更新TextView顯示Message msg=new Message();msg.what=success;msg.obj=html;handler.sendMessage(msg);}else{//更新TextView顯示Message msg=new Message();msg.what=Error;msg.obj=html;handler.sendMessage(msg);}}}).start();}/** 根據給定的path訪問網絡,去抓取html代碼* @param path* @return html* */public String getHtmlFromNet(String path) {try {URL murl=new URL(path);//創建一個URL對象HttpURLConnection conn=(HttpURLConnection) murl.openConnection();//得到URL連接對象conn.setRequestMethod("GET");//設置請求方式conn.setConnectTimeout(10000);//設置連接網絡超時時間conn.setReadTimeout(5000);//設置讀取超時時間conn.connect();//開始連接int responseCode=conn.getResponseCode();//得到服務器響應碼if(responseCode == 200){//訪問成功InputStream is=conn.getInputStream();//得到服務器返回的數據流String html=getStringFromInputStream(is);return html;}else{//訪問失敗}} catch (Exception e) {e.printStackTrace();}return null;}/** 根據流返回字符串信息* @param is* @return html* */private String getStringFromInputStream(InputStream is) throws IOException{ByteArrayOutputStream baos=new ByteArrayOutputStream();//定義一個緩存流byte[] buffer=new byte[1024];//定義一個字節數組,去讀取isint len=-1;while((len = is.read(buffer)) != -1) {//將字節寫入緩存baos.write(buffer, 0, len);}is.close();//關閉輸入流String html = baos.toString();baos.close();//關閉緩存流return html;}} 布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="網頁路徑" /><EditText android:id="@+id/edt_URL"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="http://192.168.1.106:8080/server_test/index.jsp"/><Button android:id="@+id/btn_ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查看"/><ScrollView android:layout_width="match_parent"android:layout_height="wrap_content"><TextView android:id="@+id/tv_html"android:layout_width="match_parent"android:layout_height="wrap_content"/></ScrollView> </LinearLayout>需要注意的是:在訪問自己編寫的網頁時,一般用http://locallhost:8080/工程名/文件名.jsp
在計算機中 locallhost表示本機 ? ?,再用android模擬器或真機測試時也是代表訪問本機,所以用localhost是訪問不到的。
在用模擬器時可以使用10.0.2.2(代表計算機的簽名)進行訪問。
在用真機測試時需要保證機器和計算機在同一局域網下,然后用本機的IP地址進行訪問
在命令代碼行中輸入ipconfig ?然后找到ipv4地址就是你本機的ip了,在每次關閉開啟計算機后IP地址會發生改變。
還有不要忘了添加權限啊
<uses-permission android:name="android.permission.INTERNET" />感覺我好啰嗦呀 ,哈哈總結
- 上一篇: 适用于android 4.0以上版本的子
- 下一篇: 解决客户端从服务器请求数据乱码问题