Android的HTTP通信 XML解析和异步消息处理
2010-02-03 19:10:00 来源:WEB开发网});
Button btn4 = (Button) this.findViewById(R.id.btn4);
// SAX - Simple API for XML
btn4.setText("SAX 解析 XML");
btn4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
SAXDemo();
}
});
}
// Android 调用 http 协议的 get 方法
// 本例:以 http 协议的 get 方法获取远程页面响应的内容
private void httpGetDemo(){
try {
// 模拟器测试时,请使用外网地址
URL url = new URL("http://xxx.xxx.xxx");
URLConnection con = url.openConnection();
String result = "http status code: " + ((HttpURLConnection)con).getResponseCode() + " ";
// HttpURLConnection.HTTP_OK
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer bab = new ByteArrayBuffer(32);
int current = 0;
while ( (current = bis.read()) != -1 ){
bab.append((byte)current);
}
result += EncodingUtils.getString(bab.toByteArray(), HTTP.UTF_8);
bis.close();
is.close();
textView.setText(result);
} catch (Exception e) {
textView.setText(e.toString());
}
}
// Android 调用 http 协议的 post 方法
// 本例:以 http 协议的 post 方法向远程页面传递参数,并获取其响应的内容
private void httpPostDemo(){
try {
// 模拟器测试时,请使用外网地址
String url = "http://5billion.com.cn/post.php";
Map
data.put("name", "webabcd");
data.put("salary", "100");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
ArrayList
更多精彩
赞助商链接