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;
}
*
* @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;
}