httpclient:得到状态200阿门!怎么办?

时间:2014-09-05 11:29:14

标签: java android apache http-headers

我是编程新手,请耐心等待 - 我成功完成了对网络服务的POST,但现在我想查看我收到的数据,怎么做?这是我的代码:

try {
            String data =  "{\"params\":{\"stopName\":\"%lauro linhares%\"}}";

            CredentialsProvider credProvider = new BasicCredentialsProvider();
            credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials("WKD4N7YMA1uiM8V", "DtdTtzMLQlA0hk2C1Yi5pLyVIlAQ68"));
            //
            DefaultHttpClient http = new DefaultHttpClient();
            http.setCredentialsProvider(credProvider);

            HttpPost post = new HttpPost("https://api.appglu.com/v1/queries/findRoutesByStopName/run");
            try {
                post.setEntity(new StringEntity(data, "UTF8"));
            } catch (UnsupportedEncodingException e) {
                System.out.println( "UnsupportedEncoding: "+ e);
            }

           post.addHeader("Content-Type", "application/json");
           post.addHeader("X-AppGlu-Environment", "staging");

           HttpResponse response = http.execute(post);

            System.out.println("hi! This is what we get back:"+response.getStatusLine().toString()+", "+response.getEntity().toString());
            //Hi! This is what we get back:HTTP/1.1 200 OK, org.apache.http.conn.BasicManagedEntity@4246cb88

// got status 200, which is good, but what can
//  I write next to see the 
//data i got back from web service ?



        } catch (ClientProtocolException e) {
            //
            System.out.println( "nadir Client protocol exception" + e);
        } catch (IOException e) {
            //  
            System.out.println( "nadir IOException"+ e);
        }

2 个答案:

答案 0 :(得分:0)

尝试使用:

String str =  EntityUtils.toString(response.getEntity());

答案 1 :(得分:0)

您可能想要做的就是从已执行的HttpMethod中提取响应。 HttpMethod中有一些不同的方法,但我想你只想做

String response = post.getResponseBodyAsString();