-3

I need to get data from a server via a php script I'm using an AsyncHttpClient and an AsyncHttpResponseHandler from the loopj library.

public void buttonListener (View view) {
    if (view.getId() == R.id.button) {
        //start loading...

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://host.com/data.php", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //loading succeeded

                //now I can parse the byte[] responseBody to a JSONObject...
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //loading failed
            }
        });

        //I want my program to stop at this point until onSuccess() or onFailure() is called
    }
}

At the described point in my program I want to wait until the server does response. I found some examples using Threads and the methods wait() and notifyAll() but I've no idea how to use them in my situation.

Can anybody help me?

THX

sith
  • 7
  • 1
    You should not block the UI thread, your application will stop responding and the system will offer the user a dialog to kill it – StenSoft Jan 09 '16 at 15:02
  • AsyncHttpResponseHandler means async.You should use sync. – tiny sunlight Jan 09 '16 at 15:03
  • you dont 'wait' in the UI thread. read up on 'observer' pattern.. https://en.wikipedia.org/wiki/Observer_pattern#Example ... The UI thread is one of the 'observers' that is notified onAsyncopCompletion... This happens via a 'callback' which you can code in a variety of ways in android... ( asyncTask, Handlers ) – Robert Rowntree Jan 09 '16 at 15:19
  • Please read about how thread work in Java and specially in Android – Kshitij Aggarwal Jan 09 '16 at 16:13

1 Answers1

0
public void buttonListener (View view) {
        if (view.getId() == R.id.button) {
            //start loading...

            AsyncHttpClient client = new AsyncHttpClient();
            Dialog dialog = new Dialog(MainActivity.this);
            dialog.setTitle("");
            // show a dialog that can't be close by user.
            final ProgressDialog progressDialog = new ProgressDialog(view.getContext());
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            client.get("http://host.com/data.php", new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    //loading succeeded
                    //close the dialog
                    //now I can parse the byte[] responseBody to a JSONObject...
                    progressDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    //loading failed
                    //close the dialog
                    progressDialog.dismiss();
                }
            });

            //I want my program to stop at this point until onSuccess() or onFailure() is called
        }
    }
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • THX, I already got a solution, in the end the dialog is part of my solution because the user can't do anything until the HttpClient gets a response so it works – sith Jan 11 '16 at 12:56