1

I am making an application which is depending on a web service and in order for the app to work I need to check if the phone is able to connect to that web service. I first check if the phone has any internet connection and if it does, I attempt to connect to my web service. If it can connect to the website everything works fine, but if I for some reason can't access it, if I disable the connection on my PC for instance, the app still tries connect to the website causing a force close. Is there any way to let the application try to connect to the web service for like 15 secs and if it fails to connect during this time it will stop.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if (httpzRequest() == true) {
        Toast t = Toast.makeText(this, "Success!", Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();

    } else {
        Toast t = Toast.makeText(this,
                "There's a problem with your internet connection",
                Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
    }

}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

public boolean httpzRequest() {
    if (isNetworkAvailable() == true) {
        try {

            HttpClient client = new DefaultHttpClient();
            URI site = new URI("http://www.google.se/");
            HttpGet request = new HttpGet();
            request.setURI(site);
            HttpResponse response = client.execute(request);

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e);
            return false;
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            System.out.println(e);
            return false;
        }
    } else {
        return false;
    }
}

}

Carlj901
  • 1,329
  • 4
  • 24
  • 39

3 Answers3

0

Try this with a countdown timer:

new CountdownTimer(30000, 1000) {  //This is 30 seconds so change it to 1500 to make it 15 seonds

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!"); //Here run your code.
 }
 }.start();

Also make sure you add the manifest permission for internet connection

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
0

I recommend you see this question How to set HttpResponse timeout for Android in Java

You need to set HttpConnectionParams.

Community
  • 1
  • 1
0

The one option I think is,

Take one global variable for the seconds. Initialize it with zero. Now put your webservice code in one thread inside while(true) or while(flag) loop. When you not get response because of anything, you are able to get it with catch block. Increment the global variable value there. Put thread sleep inside for the time of interval at which you calling your web service continuously. Now where you catches the exception put the if condition that if global variable and that user define variable matches then break the loop. and its done. :)

I suggest you to run web service call inside the thread as sometimes the call without thread freezes UI thread for while.

Ravi Bhatt
  • 1,930
  • 1
  • 14
  • 27