简体   繁体   中英

Get the Http response status using 'httpClient(postRequest,Handler)' method

While looking around I've found this method:

String JSONResult = httpclient.execute(request,handler); 
//Request is an HttpPost object, handler is a ResponseHandler<String>

This method made things much easier for me, i can now get the JSON response coming from my server without all these inputStream BuffredReader .... story.

But the problem is that i can't get the HttpResponse Status now like if I'd Used :

HttpResponse Response = httpclient.execute(request);
Response.getStatusLine();

Is there any way to use the first method, and still be able to get the the Response status ?

Is there any way to use the first method, and still be able to get the the Response status?

No.

Here's what you do

HttpResponse response = httpclient.execute(request);
ResponseHandler handler = ...;
String JSONResult = handler.handleResponse(response);
StatusLine status = response.getStatusLine();

Now you have access to the status from the HttpResponse object and are able to process the response with a ResponseHandler to get the json result. The point of the different methods is that you don't really care about the status, only the handled response.

You can get the status and use EntityUtils save messing around with input streams and buffered readers.

HttpResponse response = httpclient.execute(request);
String json = EntityUtils.toString(response.getEntity());
int status = response.getStatusLine().getStatusCode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM