Android: How get the status-code of an HttpClient request
This will return the int
value:
response.getStatusLine().getStatusCode()
HttpClient get status code
EDIT:
Try with:
HttpResponse httpResp = client.execute(response);
int code = httpResp.getStatusLine().getStatusCode();
The HttpStatus should be 200 ( HttpStatus.SC_OK
)
(I've read too fast the problem!)
Try with:
GetMethod getMethod = new GetMethod("http://www.example.com");
int res = client.executeMethod(getMethod);
This should do the trick!
how to get/ask only status code for a httpRequest with android?
You need to make a HEAD request. In the http protocol this will return to you only the Header of the response.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
This can be done in android using HttpHead
Finding out http response code in android
The response code can be retrieved this way:
final int statusCode = httpResponse.getStatusLine().getStatusCode();
switch (statusCode)
{
case 201:
// Successfuly Registered
break;
case 400:
// Username Already taken
break;
default:
break;
}
To list the header values, you can use:
for (final Header h : httpResponse.getAllHeaders())
{
Log.d("Responser Header", h.getName() + ": " + h.getValue()); // or h.getElements()
}
or pick the desired header directly:
final String[] contentLength = httpResponse.getHeaders("Content-Length");
and parse it.
How to get HttpClient returning status code and response body?
Don't provide the handler to execute
.
Get the HttpResponse
object, use the handler to get the body and get the status code from it directly
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
final HttpGet httpGet = new HttpGet(GET_URL);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
StatusLine statusLine = response.getStatusLine();
System.out.println(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println("Response body: " + responseBody);
}
}
For quick single calls, the fluent API is useful:
Response response = Request.Get(uri)
.connectTimeout(MILLIS_ONE_SECOND)
.socketTimeout(MILLIS_ONE_SECOND)
.execute();
HttpResponse httpResponse = response.returnResponse();
StatusLine statusLine = httpResponse.getStatusLine();
For older versions of java or httpcomponents, the code might look different.
HttpPost get HTTP response code Android
More of your code would be helpful here but the way I do it is this:
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = client.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode()
Retrieve the http code of the response in java
You can modify your code like this:
//...
HttpResponse response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//edit: there is already function for this
return EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
//Houston we have a problem
//we should do something with bad http status
return null;
}
EDIT: just one more thing ...
instead of requestToString(..);
you can use EntityUtils.toString(..);
Android HttpResponse response Code
HttpResponse.getStatusLine().getStatusCode() is what you are looking for.
Related Topics
How to Setlayoutparams() for an Imageview
How to Reference an Asset in a Library Project
How to Check If "Android.Permission.Package_Usage_Stats" Permission Is Given
Android Studio Getslotfrombufferlocked: Unknown Buffer Error
How to Filter Listview Data When Typing on Edittext in Android
How to Get Profile Like Gender from Google Signin in Android
Android: Failed to Allocate Memory
How to Save Image in Shared Preference in Android | Shared Preference Issue in Android with Image
Using the Limit Statement in a SQLite Query
Android Webview Click Open Within Webview Not a Default Browser
Bring Application to Front After User Clicks on Home Button
Continually Running Background Service
Singleton with Parameter in Kotlin
Highlighting Text Color Using HTML.Fromhtml() in Android
How to Use Room Persistence Library with Pre-Populated Database
How to Align Android Toolbar Menu/Icons to the Left Like in Google Maps App