Search code examples
androidloopjasynchttpclient

Calling a PHP in a for loop using AsyncHttpClient


I am trying to call php file inside a for loop using AsyncHttpClient after fetching the values I am storing the values in local DB.

I am sending parameters to my php file as it requires to fetch data from mySQL. Fetching part is working fine but when storing the data I am having an issue. I am actually storing the values that are received through php and then adding the parameter value too the same db table.

The problem is that when I am trying to fetch data the loops sending correct parameter value but when JSONARRAY data is received and stored to DB parameter value is always the last number in the for loop. Here is the code and example:

private void getSubMenuValues() 
{
    for (int i = 0; i < localdb.getCountValue(); i++)
    {
        String Id2 = localdb.getValue().get(i).getID();

        Log.e("DB","Value"+Id2); (All the id2 value is printed immeidately in Log.)

        parent_ID = Id2;

        Log.e("Outside Async","Value"+parent_ID);

        final RequestParams requestParams = new RequestParams();
        requestParams.put("SId", Id2);

        final String uploadWebsite = url_grant_id_details;

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler() 
        {
            private JSONArray details;
            private int success;
            private String ImageURL;
            private String IsChild;
            private String SId;
            private String SName;

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) 
            {
                try 
                {
                    alldetails = response.getJSONArray("alldetail");
                    for (int i = 0; i < details.length(); i++) 
                    {
                        JSONObject c = alldetails.getJSONObject(i);

                        SId = c.getString("SId");
                        SName = c.getString("SName");
                        IsChild = c.getString("IsChild");
                        ImageURL = c.getString("ImageURL");

                        Log.e("Inside HTTP REQUEST","Value"+parent_ID); (Value is always the last number in the loop)

                        localdb.setProfileDetails(SId, SName, IsChild, ImageURL, parent_ID);
                    }
                } 
                catch (JSONException e) 
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) 
            {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                Toast.makeText(Main.this, "Unable to connect to server. Check your network !!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Here the first log before parent_id = id2 prints all values immediately and not one by one.

Something like this:

 03-15 12:36:27.060: E/DB(16205): Value 4
 03-15 12:36:27.099: E/DB(16205): Value 3
 03-15 12:36:27.130: D/dalvikvm(16205): GC_FOR_ALLOC freed 244K, 3% free 10191K/10472K, paused 16ms, total 16ms
 03-15 12:36:27.146: E/DB(16205): Value 5
 03-15 12:36:27.154: E/DB(16205): Value 63
 03-15 12:36:27.169: E/DB(16205): Value 62
 03-15 12:36:27.185: I/Choreographer(16205): Skipped 40 frames!  The application may be doing too much work on its main thread.
 03-15 12:36:27.701: V/AsyncHttpResponseHandler(16205): Progress 1376 from 628 (219%)
 03-15 12:36:27.701: V/AsyncHttpResponseHandler(16205): Progress 1669 from 628 (266%)
 03-15 12:36:27.716: E/Inside HTTP Request(16205): Value62
 03-15 12:36:27.755: E/Inside HTTP Request(16205): Value62
 03-15 12:36:27.787: E/Inside HTTP Request16205):  Value62

If you check the log: DB Value is printed immediately 4,3,5,63,62 And then after http request it is always value 62 in the loop.

I am not sure what is exactly happening?

Can somebody help me on why this is happening and how to call Async http request inside a loop?

Thanks!


Solution

  • If you are expecting the parent_ID value to be a different number of each transaction, you should define a local variable not a global one like currently is.

    Change

    parent_ID = Id2;
    

    to

    final int parent_ID = Id2;