4

I am creating an android application but while(if?) server is not found, it will hang the tool kit (SDK/emulator?). I have attached my login code. Please help me to find the solution. I just want that if server is not available, my application will stay on at the login page without hanging itself.

when I debugged my code I didn't get value of httpResponse and after this line

HttpResponse httpResponse = httpclient.execute(httpPost);

the problem occurred

public String login(String userName, String password){
        try {
            String url = URL+ "?flag="+"on"+"&user="+userName+"&pass="+password;
            httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpclient.execute(httpPost);
            int statuscode = httpResponse.getStatusLine().getStatusCode();
            if (statuscode == 200) {
                String responseMsg = getResponse(httpResponse); 
                return responseMsg;
            }else{
                Log.e(TAG, statuscode+"");
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.getMessage(),e);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(),e);
        }
        return null;
    }
prasanna
  • 1,887
  • 3
  • 20
  • 26
Siten
  • 4,515
  • 9
  • 39
  • 64

4 Answers4

2

Check out this How to set HttpResponse timeout for Android in Java. And you really should do all your network communications in a separate thread/AsyncTask and not on your main thread.

Community
  • 1
  • 1
Audrius
  • 2,836
  • 1
  • 26
  • 35
2

If your UI on your emulator hangs you probably do network stuff on the UI thread - which will block all UI operations until the network operation is done. Read how to run this code in a thread here: http://developer.android.com/resources/articles/painless-threading.html

mreichelt
  • 12,359
  • 6
  • 56
  • 70
2

You also should consider to define a timeout (e.g. 5 seconds) so that the system does not try to connect to an unreachable server for a long time.

final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • where should i write this code.. in login method's try block?? – Siten Feb 21 '11 at 10:24
  • You can write it just after `httpClient` initialization. There is no need to set it on each request. Consider of making it configurable (via properties/preferences). – FrVaBe Feb 21 '11 at 10:44
1

Did you get an exception of some sort? If the URL isn't formatted properly you will get MalformedURLException and if the server isn't available/link is broken IOException.

You want to pass the response through a ResponseHandler

ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
   responseString = mHttpClient.execute(post, responseHandler);
   Log.d(TAG, "doHTTPpost responseString = " + responseString);
} catch (ClientProtocolException e) {
   //handle it 
} catch (IOException e) {
  //handle it
} catch (IllegalStateException e) {
  //handle it              
} finally {
  //handle it
}
prasanna
  • 1,887
  • 3
  • 20
  • 26