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)