HttpClient的Get请求

//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();

}
阅读更多
文章标签: HttpClient
想对作者说点什么? 我来说一句

HttpClient请求之基础

2016年07月12日 3.94MB 下载

HttpClient GET请求

2016年03月24日 4KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭