关闭

HttpClient的Get请求

标签: HttpClient
128人阅读 评论(0) 收藏 举报

//HttpClient的Get请求
private void getsubmit() {

    new Thread() {
        public void run() {
            try {
                path = "http://apis.haoservice.com/lifeservice/travel/scenery?pid=2&cid=45&page="
                        + page + "&key=5ca4d84913684265948925e25468b409";
                // 定义一个Http客户端对象
                HttpClient httpClient = new DefaultHttpClient();
                // 定义一个get请求的对象
                HttpGet httpGet = new HttpGet(path);
                // 执行get请求,获取响应
                HttpResponse httpResponse = httpClient.execute(httpGet);
                // 先获取状态行,再获取响应码
                int statusCode = httpResponse.getStatusLine()
                        .getStatusCode();
                if (statusCode == 200) {
                    // 请求成功
                    // 获取实体对象
                    HttpEntity entity = httpResponse.getEntity();
                    // 获取实体内容流
                    InputStream inputStream = entity.getContent();
                    String str = steamToStr(inputStream);
                    Gson gson = new Gson();
                    Xin fromJson = gson.fromJson(str, Xin.class);
                    Log.i("MainActivity", fromJson.result.get(0).address);
                    al.addAll(fromJson.result);
                    Message obtain = Message.obtain();
                    obtain.obj = fromJson;
                    obtain.what = 1;
                    handler.sendMessage(obtain);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }.start();

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6248次
    • 积分:289
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:7篇
    • 译文:0篇
    • 评论:0条