国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼實例詳解

2019-12-12 02:45:22
字體:
供稿:網(wǎng)友

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);       }     }   } } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天水市| 鄂尔多斯市| 晴隆县| 常宁市| 泰宁县| 抚州市| 汝南县| 金堂县| 华容县| 无极县| 甘南县| 高邮市| 广河县| 湖口县| 岑巩县| 锡林浩特市| 清水县| 开远市| 绥德县| 洛浦县| 凤冈县| 曲阜市| 宁乡县| 南陵县| 彩票| 衡山县| 闻喜县| 黔西县| 秦皇岛市| 旺苍县| 保德县| 西和县| 兴安盟| 博白县| 盐城市| 双鸭山市| 绥江县| 资中县| 木兰县| 临夏县| 鱼台县|