0

I wrote a asynctask to process json,The json result is very long (need 40 - 70 seconds to process on 2G).The asynctask publishProgress(""); function execute only after fetching and processing the json ( just before postexecute() ). How can i update each second.

    class LoadAllProducts extends AsyncTask<String, String, String> {
        int per=100;
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            clear();
            pDialog = new ProgressDialog(Load.this);
            pDialog.setMessage("Loading ... Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }


        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONObject json = jParser.makeHttpRequest(url, "POST", params);
            publishProgress("");
            if(json!=null) {
                try {
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1)
                    {
                        products = json.getJSONArray(TAG_PRODUCTS);

                        for (int i = 0; i < products.length(); i++) 
                        {
                            JSONObject c = products.getJSONObject(i);

////////////////////////////////// Other codes
                        }
                    } else {}
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        protected void onPostExecute(String file_url)
        {
            pDialog.dismiss();      load(0);

        }
        @Override
        protected void onProgressUpdate(String... text)
        {
            per=per-1;
            pDialog.setMessage("Loading Questions... Please wait..."+per);


        }

    }

I refered : Android, java - Unable to update variable from asynctask

Instance variable of Activity not being set in onPostExecute of AsyncTask or how to return data from AsyncTask to main UI thread

Community
  • 1
  • 1
Sigma Equties
  • 67
  • 2
  • 10

2 Answers2

1

Use AsyncHttpClient for process with progress

in your .gradle file add compile 'com.loopj.android:android-async-http:1.4.6'

Now create a service class and declare it in your androidmanifest.xml

Next in onStartCommand in your service class:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG, "onStartCommand");

    try {

        AsyncHttpClient client = new AsyncHttpClient();
        client.setURLEncodingEnabled(true);

        client.post(this, url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                  // you code here after the work is done                       

                stopService();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                //your code here
                stopService();

            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                super.onProgress(bytesWritten, totalSize);
                Log.e("Progess", "" + (bytesWritten / totalSize) * 100 + "%");

            }
        });


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

    return super.onStartCommand(intent, flags, startId);
}

and from your activity call

startService(serviceIntent);
Ibrahim Gharyali
  • 544
  • 3
  • 22
1

Publish your progress by using publishProgress from doInBackground() like

protected Void doInBackground(Integer... params) 
{
    publishProgress("Loading Questions... Please wait..."+per);
    ...
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68