网络请求

来源:互联网 发布:mysql type ref 编辑:程序博客网 时间:2024/06/06 00:25

//自定义函数

public void getYanZhengMa(String url1) {
final String url = url1;
new Thread() {//新起线程实现网络请求
public void run() {
// 响应
HttpResponse mHttpResponse = null;
// 实体
HttpEntity mHttpEntity = null;
// 生成一个请求对象
HttpGet httpGet = new HttpGet(url);
// 生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 下面使用Http客户端发送请求,并获取响应内容

InputStream inputStream = null;
try {
// 发送请求并获得响应对象
mHttpResponse = httpClient.execute(httpGet);
// 获得响应的消息实体
mHttpEntity = mHttpResponse.getEntity();


// 获取一个输入流
inputStream = mHttpEntity.getContent();


BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));


String result = "";
String line = "";
while (null != (line = bufferedReader.readLine())) {
result += line;
}


// 将结果打印出来,可以在LogCat查看
System.out.println(result);
} catch (Exception e) {
System.out.println("chu cuo ");
}
};
}.start();
}

0 0
原创粉丝点击