0

I have the following method:

     public static boolean getIsUp()
     {
         String method = "isup.php";
         AsyncHttpResponseHandler response = new AsyncHttpResponseHandler(){

         @Override
             public void onSuccess(String content) {
            if(Integer.parseInt(content) == IS_UP_CONTENT)
                //code to return true
            else
                //code to return false
        }

     };
     get(method, null, response);
 }

How would you fill the //code to return true and //code to return false so that the method return the appropriate response? Is that even possible?

Martin Golpashin
  • 1,032
  • 9
  • 28
  • 3
    Before you continue, read [this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Sotirios Delimanolis Jan 29 '14 at 02:39
  • And on top of reading that... instead of `if (condition) else` and writing what will be nearly identical code twice... why don't you just return what the (correct) comparison will evaluate to? – nhgrif Jan 29 '14 at 02:42
  • @SotiriosDelimanolis Ooops, there was supposed to be an Integer.parseInt(content) instead of just content. – Mustafa Taleb Jan 29 '14 at 02:43
  • Do you have to use `AsyncHttpResponseHandler`. Does it provide a callback mechanism? Can you instead use a `ScheduledExecutorService` and block on the returned `Future`? – Sotirios Delimanolis Jan 29 '14 at 02:51
  • @SotiriosDelimanolis I am using loopj's AsyncHttpClient and it uses the AsyncHttpResponseHandler. I just took a look at the ScheduledExecutorService, and it seems like a hacky solution. But maybe that is because I am a C# developer and we are just used to the async and await keywords that basically does all this for you. – Mustafa Taleb Jan 29 '14 at 02:59
  • That's what `Future.get()` does. It waits until the `Callable` you submit to the `ExecutorService` has finished and returned a value. Maybe `loopj` has corresponding behavior. I've never used it. – Sotirios Delimanolis Jan 29 '14 at 03:00
  • Hmmm. I'l make sure to look into that, thanks! – Mustafa Taleb Jan 29 '14 at 03:09

1 Answers1

0

I think you should not use an AsyncHttpResponseHandler inside a non-async method if you want to instantly return an result.

What you can do is using an AsyncTask and the onPostExecute Callback

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Community
  • 1
  • 1
Martin Golpashin
  • 1,032
  • 9
  • 28