Search code examples
javaandroidandroid-async-httploopj

(Android | Java) AndroidAsyncHttp (Loopj) to complete web request before proceeding thread


I'm currently requesting an JsonObject via AsyncHttpClient (loopj) web request, however the request is rather slow and I need the Object to proceed. Currently the program crashes as the object being saved is empty and the save function gets called before the web request is complete.

Here's a snippet of my code of what i'm trying to do:

   btnCreate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            AsyncHttpClient client = new AsyncHttpClient();
            String apiAddress = MYAPIADDRESS;

            client.get(apiAddress,
                    new JsonHttpResponseHandler() {

                        //@Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                            try {

                                JSONObject tempTransition = response.getJSONArray("items").getJSONObject(0).getJSONObject("snippet");

                                Toast.makeText(getApplicationContext(), tempTransition.get("description").toString(), Toast.LENGTH_SHORT).show();

                                if(!dirDirectoryJson.contains(tempTransition)){
                                    dirDirectoryJson.add(tempTransition);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {

                        }

                        @Override
                        public void onFinish() {
                            // Completed the request (either success or failure)

                        }

                    });

                //*** Crashes here ****
                //dirDirectoryJson returning null object from above due to call before complete web request and crashes the program
                try {

                    getDescription = dirDirectoryJson.get(0).getString("description").toString().trim();
                    setDescription( getDescription ); 

               } catch (JSONException e) {
                    e.printStackTrace();
            }
     }

I've tried SyncHttpClient, make thread sleep etc. as well as seen/tried these (and many more other) links,

How to wait for async http to end before continuing?

https://forum.qt.io/topic/5389/how-to-ensure-fully-asynchronous-web-requests

How to wait for all requests to finish

How to make the Main thread wait, when i dont know when will Async task finish the job.?

I've also tried to toast the object received while commenting out the storing portion of the code. (commented above) The code didn't crash but the toast only appear after awhile when the web request is complete.

How do I go about resolving this issue? (i.e. successfully store the object without crashing the app only after the web request is complete). I've been searching very very long on the web but fail to get a workable method and i'm kinda a novice in Android.

Do kindly help and let me know if more details is required. Thanks in advance!


Solution

  • Since the request you are doing is async, you should do the logic of using the response of the request in its completion handler.

    Change your code to something like

    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
    
     try {
    
         JSONObject tempTransition = response.getJSONArray("items").getJSONObject(0).getJSONObject("snippet");
    
         Toast.makeText(getApplicationContext(), tempTransition.get("description").toString(), Toast.LENGTH_SHORT).show();
    
         if(!dirDirectoryJson.contains(tempTransition)){
               dirDirectoryJson.add(tempTransition);
         }
    
         getDescription = dirDirectoryJson.get(0).getString("description").toString().trim();
                    setDescription( getDescription );
       } catch (JSONException e) {
          e.printStackTrace();
       }
    }
    

    Also make your variables as instance var (that is declare them at class scope) if shows cant access non final variables within the handler