【问题标题】:httpclient: got status 200 amen! now what?httpclient:获得状态 200 阿门!怎么办?
【发布时间】:2014-09-05 11:29:14
【问题描述】:

我是编程新手,请耐心等待 - 我成功地对 Web 服务进行了 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);
        }

【问题讨论】:

    标签: java android apache http-headers


    【解决方案1】:

    尝试使用:

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

    【讨论】:

      【解决方案2】:

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

      String response = post.getResponseBodyAsString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2021-05-25
        • 1970-01-01
        • 2015-09-22
        相关资源
        最近更新 更多