如何使用ProgressDialog显示JSON解析进度? [英] How to use ProgressDialog to show JSON parsing progress?

查看:91
本文介绍了如何使用ProgressDialog显示JSON解析进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用进度条显示一些 JSON 解析的进度.我从未使用过它,并在互联网上找到了一些示例.所以,我试图意识到这一点,但解析开始时应用程序崩溃.这是代码:

I want to show progress of some JSON parsing using with progress bar. I've never used it and found some examples in the Internet. So, I try to realize it but application crashes when parsing starts. Here is code:

public class Parser extends Activity {

public static String w_type1 = "news";
public static String w_type2 = "events_put";
public ListView lv;
ArrayList<Widget> data = new ArrayList<Widget>();
WidgetAdapter wid_adptr = new WidgetAdapter(this, data);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parser);


    lv = (ListView) this.findViewById(R.id.list);
    lv.setAdapter(wid_adptr);
    new ParseTask().execute();

}

private class ParseTask extends AsyncTask<Void, Void, String> {
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resultJson = "";

    public ProgressDialog dialog;
    Context ctx;

    protected void onPreExecute() {
        dialog = new ProgressDialog(ctx);
        dialog.setMessage("Pasring...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL("http://api.pandem.pro/healthcheck/w/");

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();

            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            resultJson = buffer.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultJson;
    }

    @Override
    protected void onPostExecute(String strJson) {
        super.onPostExecute(strJson);


        JSONObject dataJsonObj = null;

        try {
            dataJsonObj = new JSONObject(strJson);
            JSONArray widgets = dataJsonObj.getJSONArray("widgets");

            for (int i = 0; i < widgets.length(); i++) {
                JSONObject widget = widgets.getJSONObject(i);

                String wType = widget.getString("type");

                if (wType.equals(w_type1) || wType.equals(w_type2)) {

                    String title = widget.getString("title");
                    String desc = widget.getString("desc");
                    String img_url = "";
                    if (widget.has("img")) {
                        JSONObject img = widget.getJSONObject("img");
                        img_url = img.getString("url");
                    }
                    data.add(new Widget(wType, title, desc, img_url));
                    //wid_adptr.notifyDataSetChanged();
                }
            }

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

        dialog.dismiss();
    }
}
}

如果我不使用 ProgressDialog(只是评论或删除对话框代码)应用程序正常工作.我该如何解决?

If i don't use ProgressDialog (just comment or delete dialog code) application works correctly. How can I fix it?

推荐答案

没有任何 logcat 很难提供帮助,但是您的 ctx 似乎为 null 所以

Without any logcat it's difficult to help, but it seems that your ctx is null so

dialog = new ProgressDialog(ctx);

无法创建对话框.

尝试将构造函数添加到 AsyncTask 并在此处传递上下文,例如:

Try to add constructor to the AsyncTask and pass the context here, something like:

private class ParseTask extends AsyncTask<Void, Void, String> {
...
    public ParseTask(Context ctx) {
        this.ctx = ctx;
    }
...
}

开始任务:

new ParseTask(this).execute();

这篇关于如何使用ProgressDialog显示JSON解析进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆