使用HttpClient框架发送Get请求
//1.创建client对象
HttpClient client = new DefaultHttpClient();
//2.创建get请求对象
HttpGet get = new HttpGet(path);
//3.使用client发送get请求
HttpResponse response = client.execute(get);
//获取状态行
StatusLine line = response.getStatusLine();
//4.获取状态码
int code = line.getStatusCode(); //5.获取实体
HttpEntity entity = response.getEntity();
小部分代码:
HttpClient client = new DefaultHttpClient();
//2.创建get请求对象
HttpGet get = new HttpGet(path);
try {
//3.使用client发送get请求
HttpResponse response = client.execute(get);
//获取状态行
StatusLine line = response.getStatusLine();
//获取状态码
int code = line.getStatusCode();
if(code == 200){
//获取实体
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String text = Tools.getTextFromStream(is);
Message msg = handler.obtainMessage();
msg.obj = text;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}