Android测试Http网络请求。

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


你可能感兴趣的:(android,String,网络,测试,null,url)