Android測試Http網路請求。

longerandlonger發表於2012-09-27
try {
	URL url = new URL("http://love.taobao.com/ajax/activity/get_activity.do?id=1");
	java.net.URLConnection conn = url.openConnection();
	conn.setDoInput(true);
	conn.setDoOutput(true);
	InputStream inputStream = conn.getInputStream();
	InputStreamReader isReader = new InputStreamReader(inputStream, "GBK"); // 返回的編碼格式
	BufferedReader bfReader = new BufferedReader(isReader);
	StringBuilder sb = new StringBuilder();
	for (String tmp=null; (tmp=bfReader.readLine())!=null; ) {
		sb.append(tmp);
	}
	tv.setText(sb);
} catch (MalformedURLException e) {
	e.printStackTrace();
} catch (Throwable e) {
	e.printStackTrace();
}

中文亂碼問題,參考:http://nneverwei.iteye.com/blog/490884

android.os.NetworkOnMainThreadException 問題,參考:http://geeksun.iteye.com/blog/1447708


Some Code:

InetAddress inetAddress = InetAddress.getByName("love.taobao.com");


相關文章