从Apache Http POST请求返回字符串(Android)

时间:2018-10-25 21:39:14

标签: java android json apache post

我正在尝试从Andorid应用中的HTTP发布请求中获取json 字符串。使用this post的解决方案,代码也显示在这里。

public void post(String completeUrl, String body) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(completeUrl);
httpPost.setHeader("Content-type", "application/json");
try {
    StringEntity stringEntity = new StringEntity(body);
    httpPost.getRequestLine();
    httpPost.setEntity(stringEntity);

    httpClient.execute(httpPost);
} catch (Exception e) {
    throw new RuntimeException(e);
}
}

我从异步任务内部调用帖子(用于处理单独线程上的网络访问)。

String result;
result = post("https://StringURLGoesHere.com/", "jsonStringBodyGoesHere");

根据documentation for HttpClient class,要处理响应,我需要向HttpClient.execute()方法添加第二个参数ResponseHandler。

public interface ResponseHandler<T> {
T handleResponse(HttpResponse var1) throws ClientProtocolException, IOException;
}

我是这样做的:

public String post(String completeUrl, String body) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(completeUrl);
        httpPost.setHeader("Content-type", "application/json");
        try {
            StringEntity stringEntity = new StringEntity(body);
            httpPost.getRequestLine();
            httpPost.setEntity(stringEntity);

            ResponseHandler<String> reply = new ResponseHandler<String>() {
                @Override
                public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
                    return httpResponse.toString();
                }
            };
            return httpClient.execute(httpPost,reply);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

我在应用程序的textview中显示字符串。内容为:

org.apache.http.message.BasicHttpResponse@1446ef0c

org.apache.http.message.BasicHttpResponse@b83bd3d

org.apache.http.message.BasicHttpResponse@1c4c9e1d

,依此类推。

为什么我会以字符串形式返回此返回?为了使json对象的字符串在发布后返回,应该进行哪些更改?

2 个答案:

答案 0 :(得分:0)

Try like below to capture HttpRepose to see your response;
HttpClient request = HttpClientBuilder.create().build();        
HttpGet get = new HttpGet(url);
get.setHeader( "Authorization", token);
HttpResponse response = null;
try {
     response = request.execute( get );
} catch ( ClientProtocolException e ) {
     // TODO Auto-generated catch block
     e.printStackTrace();
} catch ( IOException e ) {
    // TODO Auto-generated catch block
     e.printStackTrace();
}

BufferedReader rd = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) );

StringBuffer result = new StringBuffer();
String line = "";
while ( (line = rd.readLine()) != null ) {
      result.append( line );
}

System.out.println( response.getStatusLine().getStatusCode() );
System.out.println( result.toString() );

答案 1 :(得分:0)

您可以这样:

    httpPost.setEntity(entity);
    HttpResponse response = httpclient.execute(httpPost);
    String responseString = new BasicResponseHandler().handleResponse(response);
    return responseString;  //this is your want