3

I want to check if a web service is available before connecting to it, so that if it is not available, I can display a dialog that says so. My first attempt is this:

public void isAvailable(){

    // first check if there is a WiFi/data connection available... then:

    URL url = new URL("URL HERE");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Connection", "close");
    connection.setConnectTimeout(10000); // Timeout 10 seconds
    connection.connect();

    // If the web service is available
    if (connection.getResponseCode() == 200) {
        return true;
    }
    else return false;
}

And then in a separate class I do

if(...isAvailable()){
    HttpPost httpPost = new HttpPost("SAME URL HERE");
    StringEntity postEntity = new StringEntity(SOAPRequest, HTTP.UTF_8);
    postEntity.setContentType("text/xml");
    httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
    httpPost.setEntity(postEntity);

    // Get the response
    HttpClient httpclient = new DefaultHttpClient();
    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httpPost);

    // Convert HttpResponse to InputStream for parsing
    HttpEntity responseEntity = httpResponse.getEntity();
    InputStream soapResponse = responseEntity.getContent();

    // Parse the result and do stuff with the data...
}

However I'm connecting to the same URL twice, and this is inefficient and probably slowing down my code.

First of all, is it?

Secondly, what's a better way to do this?

Kalina
  • 5,504
  • 16
  • 64
  • 101

2 Answers2

3

I would just try connecting, if it times out, handle the exception, and display an error.

dardo
  • 4,870
  • 4
  • 35
  • 52
  • What do I check for to see if it times out? The exceptions I'm catching are `UnsupportedEncodingException`, `ClientProtocolException`, `IOException` `ParserConfigurationException`, and `SAXException`... – Kalina Oct 16 '12 at 17:17
  • 1
    Believe the timeout exception derives from IOException, I'd look at this wiki post: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests, if all else fails, try entering a bogus URL and see what exception is thrown. – dardo Oct 16 '12 at 17:21
  • You're right, it's IOException. However, now that I tried this, I remember why I didn't do it this way in the first place... Because I can't show a dialog from a non-UI thread... That's a different issue though, so thanks! – Kalina Oct 16 '12 at 17:27
  • Android should have a managed "session" like context that you could probably populate on an error. Also, accept the answer if it solved the problem. – dardo Oct 16 '12 at 17:29
0

May be you should consider looking at HttpConnectionParams class methods.

public static void setConnectionTimeout (HttpParams params, int timeout)

Sets the timeout until a connection is etablished. A value of zero means the timeout is not used. The default value is zero.

public static void setSoTimeout (HttpParams params, int timeout)

Sets the default socket timeout (SO_TIMEOUT) in milliseconds which is the timeout for waiting for data. A timeout value of zero is interpreted as an infinite timeout. This value is used when no socket timeout is set in the method parameters.

Hope this helps.

Jigish Chawda
  • 2,148
  • 1
  • 22
  • 29