Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼實(shí)例詳解
Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼
1.添加網(wǎng)絡(luò)權(quán)限
<!--訪問網(wǎng)絡(luò)的權(quán)限--> <uses-permission android:name="android.permission.INTERNET"/>
2.獲取網(wǎng)絡(luò)中網(wǎng)頁的數(shù)據(jù)
/**
* 獲取網(wǎng)頁HTML源代碼
* @param path 網(wǎng)頁路徑
*/
public static String getHtml(String path) throws Exception {
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inStream=conn.getInputStream();
byte[] data=read(inStream);
String html=new String(data,"UTF-8");
return html;
}
return null;
}
/**
* 讀取流中的數(shù)據(jù)
*/
public static byte[] read(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
outputStream.write(b);
}
inputStream.close();
return outputStream.toByteArray();
}
3.處理查看網(wǎng)頁源碼的控制
public class HtmlViewActivity extends Activity {
private EditText pathText;
private TextView codeView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText=(EditText) findViewById(R.id.pagepath);//網(wǎng)頁路徑
codeView=(TextView)findViewById(R.id.codeView);//顯示獲得的源碼
Button button=(Button) findViewById(R.id.button);//查看按鈕
button.setOnClickListener(new ButtonClickListener());//按鈕事件
}
/**
* 查看按鈕處理事件
*/
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=pathText.getText().toString();
try {
String html=PageService.getHtml(path);
codeView.setText(html);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1);
}
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)的簡單計(jì)算器功能【附完整demo源碼下載】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的簡單計(jì)算器功能,結(jié)合實(shí)例形式分析了Android計(jì)算器的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)帶節(jié)點(diǎn)的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶節(jié)點(diǎn)的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法,結(jié)合實(shí)例形式分析了Android ImageView界面布局及元素屬性動(dòng)態(tài)操作兩種功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02
Android開發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹
這篇文章主要介紹了Android開發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹,本文直接用圖片說明Genymotion的安裝和模擬效果,并提供官網(wǎng),需要的朋友可以參考下2015-04-04
Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android 應(yīng)用啟動(dòng)歡迎界面廣告的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 應(yīng)用啟動(dòng)歡迎界面廣告的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android通過命令連接wifi的方法(解決usb不能用問題)
這篇文章主要介紹了Android通過命令連接wifi的方法(解決usb不能用的情況),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Android實(shí)現(xiàn)兩臺(tái)手機(jī)屏幕共享和遠(yuǎn)程控制功能
在遠(yuǎn)程協(xié)助、在線教學(xué)、技術(shù)支持等多種場景下,實(shí)時(shí)獲得另一部移動(dòng)設(shè)備的屏幕畫面,并對(duì)其進(jìn)行操作,具有極高的應(yīng)用價(jià)值,本項(xiàng)目旨在實(shí)現(xiàn)兩臺(tái) Android 手機(jī)之間的屏幕共享與遠(yuǎn)程控制,需要的朋友可以參考下2025-04-04

