0

I am developing one application which is connecting to server to get some data.
In this I want to check first if application is connected to server or not. And then, if server is on or off?
Based on the result I want to do my further manipulations.

So how do I get the result of server status?

Here is the code which I am using:

Code:

try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://192.168.1.23/sip_chat_api/getcountry.php");

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

            } catch (Exception e) {
 }
Bo.
  • 2,547
  • 3
  • 24
  • 36
Juned
  • 6,290
  • 7
  • 45
  • 93
  • look at this class http://docs.oracle.com/javase/1.4.2/docs/api/java/net/HttpURLConnection.html – Sunny Jun 04 '12 at 11:42

2 Answers2

1

Maintaining session cookies is best choice here, please see how to use session cookie: How do I make an http request using cookies on Android?

here, before sending request to server, check for session cookie. If it exists, proceed for the communication. Update: The Java equivalent -- which I believe works on Android -- should be:

InetAddress.getByName(host).isReachable(timeOut)

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
1

Check getStatusLine() method of HttpResponse

any status code other than 200 means there is a problem , and each status codes points to different problems happened.

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpResponse.html?is-external=true

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/StatusLine.html#getStatusCode()

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42