Juned
Juned

Reputation: 6346

determine aplication is connected to server or not

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) {
 }

Upvotes: 0

Views: 166

Answers (2)

Subin Sebastian
Subin Sebastian

Reputation: 10997

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()

Upvotes: 1

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

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)

Upvotes: 1

Related Questions