androidandroid-asynctaskandroid-progressbarandroid-jsonandroid-parser

JSON Parser using asynctask with progress bar


JSON Parser using asynctask with progress bar. What is the right way to do this?

I applied and adapted what I read from tuts, but it returns null when returning data from the downloadTask

Here is my downloadTask task:

private class DownloadTask extends AsyncTask<Void, Integer, Void> {

    private ProgressDialog dialog;

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.setProgress(values[0]);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.dialog = new ProgressDialog(ma);
        this.dialog.setIndeterminate(false);
        this.dialog.setMax(100);
        this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        this.dialog.setCancelable(false);
        this.dialog.setTitle(R.string.updating);
        this.dialog.setMessage(getString(R.string.downloading));
        this.dialog.show();

        // progress = 0;
    }

    @Override
    protected String doInBackground(String... arg0) {

        int count;
        byte data[] = null;
                url = "http://arsonicdemo.digify.com.ph/webapi/campaigns?pf=1";

            try {
                URL url1 = new URL(url);
                URLConnection conexion = url1.openConnection();
                conexion.connect();
                int lenghtOfFile = conexion.getContentLength();
                InputStream input = new BufferedInputStream(url1.openStream());
                campaignObj = new JSONObject(parser(url));
                populateCampaigns(campaignObj);
                appClass.updateList();
                data = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress((int) (total * 100 / lenghtOfFile));
                }
                input.close();
            } catch (Exception e) {
            }
        return new String(data);
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog.dismiss();
        Toast.makeText(ma, "Update Successful", Toast.LENGTH_SHORT).show();
        ma.initMarkers();
    }
}

This is basically populateCampaigns method:

public void populateCampaigns(String url) throws JSONException {

    campaignObj = new JSONObject(parser(url));
    campaignData = campaignObj.getJSONObject("data");
}

And lastly, here is parser method:

public static String parser(String url) {
    StringBuilder builder = new StringBuilder();
    System.setProperty("http.keepAlive", "false");
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    final HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000);
    client = new DefaultHttpClient(httpParams);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("CAMPAIGN DOWNLOADER ERROR", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return builder.toString();
}

Solution

  • I finally figured it out, here's my whole asyncTask code, any comments or suggestions are highly appreciated:

    private class DownloadTask extends AsyncTask<Void, Integer, Void> {
    
        private ProgressDialog dialog;
    
        @Override
        protected void onProgressUpdate(Integer... progress) {
            int current = progress[0];
            int total = progress[1];
    
            float percentage = 100 * (float) current / (float) total;
    
            dialog.setProgress((int) percentage);
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            this.dialog = new ProgressDialog(ma);
            this.dialog.setIndeterminate(false);
            this.dialog.setMax(100);
            this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            this.dialog.setCancelable(false);
            this.dialog.setTitle(R.string.updating);
            this.dialog.setMessage(getString(R.string.downloading));
            this.dialog.show();
    
            // progress = 0;
        }
    
        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                timestampPref = appClass.getSharedStringPref("updateTimestamp", null);
    
                if (null != timestampPref) {
                    url = getResources().getString(R.string.updateUrl1) + timestampPref + getResources().getString(R.string.updateUrl2);
                    Log.i("URL", url);
                } else {
                    url = getResources().getString(R.string.firstUrl);
                    Log.i("URL", url);
                }
                downloadCampaigns: {
                    try {
    
                        System.setProperty("http.keepAlive", "false");
                        campaignObj = new JSONObject(parser(url));
    
                        try {
                            campaignData = campaignObj.getJSONObject("data");
                            dataLength = campaignData.length();
    
                            for (int x = 0; x < dataLength; x++) {
                                if (reachable()) {
                                    campaignCtr = campaignData.getJSONObject(String.valueOf(x));
                                    tempType = campaignCtr.getString("template_type");
                                    populateCampaigns(campaignCtr, tempType);
    
                                    publishProgress(x, dataLength);
    
                                } else {
                                    // AlertDialog.Builder builder = new
                                    // AlertDialog.Builder(ma);
                                    // builder.setTitle("NO INTERNET");
                                    // builder.setMessage("Please turn on wifi or cellular data in your mobile Settings to continue.");
                                    // builder.setPositiveButton("Okay", null);
                                    // builder.show();
                                    break downloadCampaigns;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
                        updateTimestamp = campaignObj.getString("date");
                        updateTimestamp = updateTimestamp.substring(0, updateTimestamp.indexOf(" "));
                        appClass.setSharedStringPref("updateTimestamp", updateTimestamp);
                        Log.i("UPDATETIMESTAMP", updateTimestamp);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            } catch (Exception e) {
                Log.e("UPDATE ERROR", e.toString());
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            appClass.updateList();
            dialog.dismiss();
            Toast.makeText(ma, "Update Successful", Toast.LENGTH_SHORT).show();
            ma.initMarkers();
        }
    }