日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网页源代码查看器

發(fā)布時(shí)間:2024/4/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网页源代码查看器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這篇博文適用于剛剛接觸移動(dòng)開發(fā)利用網(wǎng)絡(luò)連接加載數(shù)據(jù)的小伙伴們,

分廢話不多說,直接貼代碼。

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();//訪問網(wǎng)絡(luò)時(shí)new一個(gè)子線程new Thread(new Runnable() {@Overridepublic void run() {//請(qǐng)求網(wǎng)絡(luò)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();}/** 根據(jù)給定的path訪問網(wǎng)絡(luò),去抓取html代碼* @param path* @return html* */public String getHtmlFromNet(String path) {try {URL murl=new URL(path);//創(chuàng)建一個(gè)URL對(duì)象HttpURLConnection conn=(HttpURLConnection) murl.openConnection();//得到URL連接對(duì)象conn.setRequestMethod("GET");//設(shè)置請(qǐng)求方式conn.setConnectTimeout(10000);//設(shè)置連接網(wǎng)絡(luò)超時(shí)時(shí)間conn.setReadTimeout(5000);//設(shè)置讀取超時(shí)時(shí)間conn.connect();//開始連接int responseCode=conn.getResponseCode();//得到服務(wù)器響應(yīng)碼if(responseCode == 200){//訪問成功InputStream is=conn.getInputStream();//得到服務(wù)器返回的數(shù)據(jù)流String html=getStringFromInputStream(is);return html;}else{//訪問失敗}} catch (Exception e) {e.printStackTrace();}return null;}/** 根據(jù)流返回字符串信息* @param is* @return html* */private String getStringFromInputStream(InputStream is) throws IOException{ByteArrayOutputStream baos=new ByteArrayOutputStream();//定義一個(gè)緩存流byte[] buffer=new byte[1024];//定義一個(gè)字節(jié)數(shù)組,去讀取isint len=-1;while((len = is.read(buffer)) != -1) {//將字節(jié)寫入緩存baos.write(buffer, 0, len);}is.close();//關(guān)閉輸入流String html = baos.toString();baos.close();//關(guān)閉緩存流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="網(wǎng)頁路徑" /><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>
需要注意的是:在訪問自己編寫的網(wǎng)頁時(shí),一般用http://locallhost:8080/工程名/文件名.jsp

在計(jì)算機(jī)中 locallhost表示本機(jī) ? ?,再用android模擬器或真機(jī)測(cè)試時(shí)也是代表訪問本機(jī),所以用localhost是訪問不到的。

在用模擬器時(shí)可以使用10.0.2.2(代表計(jì)算機(jī)的簽名)進(jìn)行訪問。

在用真機(jī)測(cè)試時(shí)需要保證機(jī)器和計(jì)算機(jī)在同一局域網(wǎng)下,然后用本機(jī)的IP地址進(jìn)行訪問


在命令代碼行中輸入ipconfig ?然后找到ipv4地址就是你本機(jī)的ip了,在每次關(guān)閉開啟計(jì)算機(jī)后IP地址會(huì)發(fā)生改變。


還有不要忘了添加權(quán)限啊

<uses-permission android:name="android.permission.INTERNET" />感覺我好啰嗦呀 ,哈哈

總結(jié)

以上是生活随笔為你收集整理的网页源代码查看器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。