David
David

Reputation: 5

get the HTTP status server

Hi I am developing an android application, I like to create a class to get the HTTP status before send the data to the server with HTTP Post.

Have any form to get the HTTP status of this server?

I read to get the 200 code is the server is running and another code no

Thanks.

Resolved the timeout is very long, My solution is:

            HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 10000);
    HttpClient httpclient = new DefaultHttpClient(params);

and then: HttpGet httpRequest = new HttpGet(server); HttpResponse response = httpclient.execute(httpRequest);

Upvotes: 0

Views: 754

Answers (2)

blganesh101
blganesh101

Reputation: 3695

This is how you get Response code if you are using HttpUrlConnection :
when server is not running

  int status = ((HttpURLConnection) connection).getResponseCode();
  Log.i("", "Status : " + status);

And here is if you are using HttpClient :

 HttpResponse response = httpclient.execute(httppost);
 Log.w("Response ","Status line : "+ response.getStatusLine().toString());

Upvotes: 0

blganesh101
blganesh101

Reputation: 3695

You could do the following

HttpGet httpRequest = new HttpGet(myUri);
HttpEntity httpEntity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
response.getStatusLine().getStatusCode()

Upvotes: 1

Related Questions