本文實例講述了Android編程獲取網址HTML代碼的方法。分享給大家供大家參考,具體如下:
/**** @param aUrl 網址* @param aEncode 編碼* @return 返回的HTML代碼* @throws Exception 對外拋出異常*/public String getHTML(String aUrl, String aEncode) throws Exception { URL url = new URL(aUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200){ InputStream inputStream = conn.getInputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len = inputStream.read(buffer)) != -1){ outStream.write(buffer, 0, len); } String htmlStr = new String(outStream.toByteArray(), aEncode); inputStream.close(); outStream.close(); return htmlStr; } return null;}更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作json格式數據技巧總結》、《Android編程之activity操作技巧總結》、《Android開發入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答