JSON Parser使用asynctask和进度条

时间:2014-06-23 02:29:10

标签: android android-asynctask android-progressbar android-json android-parser

JSON Parser使用asynctask和进度条。这样做的正确方法是什么?

我应用并调整了我从tuts中读取的内容,但是从downloadTask返回数据时返回null

这是我的downloadTask任务:

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();
    }
}

这基本上是populateCampaigns方法:

public void populateCampaigns(String url) throws JSONException {

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

最后,这是解析器方法:

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();
}

2 个答案:

答案 0 :(得分:0)

您正尝试从网址下载两次。首先,您开始使用UrlConnection开始下载,然后调用parser方法,该方法不解析但使用HttpClient从该网址下载。所有人都很混淆。

答案 1 :(得分:0)

我终于明白了,这是我的整个asyncTask代码,我们非常感谢任何意见或建议:

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();
    }
}