android - AsyncTask、doinBackground 永远不会完成

标签 android mysql android-asynctask

在我的代码中,我使用 json 代码从在线 MySQL 数据库获取数据。 问题是,在某些情况下,进度对话框永远不会完成,只是不断循环而不获取任何数据!并不总是这样做,在大多数情况下它就像一个魅力。

这是代码,有什么想法吗!!!

class LoadAllProducts extends AsyncTask<String, String, String> {
    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllCategories.this);
        pDialog.setMessage("loading all categories");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) 
    {

        String url_all_categories = "http://www.*****.com/****/****.php";

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.makeHttpRequest(url_all_categories,
                "GET", params);

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                categories = json.getJSONArray(TAG_CATEGORIES);

                for (int i = 0; i < categories.length(); i++) {
                    JSONObject c = categories.getJSONObject(i);
                    id = c.getString(TAG_CID);
                    name = c.getString(TAG_NAME);
                    descAR = c.getString(TAG_DESC_AR);
                    picture = c.getString(TAG_PICTURE);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_CID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_DESC_AR, descAR);
                    map.put(TAG_PICTURE, picture);

                    categoriesList.add(map);
                }
            } else {
                Intent i = new Intent(getApplicationContext(),
                        NewCategory.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();
        adapter = new LazyAdapter(AllCategories.this, categoriesList);
        setListAdapter(adapter);
    }
}

最佳答案

替换pDialog.dismiss();

 pDialog.cancel();

关于android - AsyncTask、doinBackground 永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472526/

相关文章:

android - 如何在httpurlconnection中发送带参数的get请求?

android - 从 Android NDK list 文件中引用相对路径资源

java - Android AsyncTask-to-Activity 回调 nullPointerException

java - Android从AsyncTask调用notifyDataSetChanged

android - 使用 AsyncTaskLoader 填充 ListView

android - 如何查看哪个用户在 Google Play 中上传了 apk 文件?

mysql - 选择计数(*)... mysql

mysql数据库导入特别慢

MySQL 左外连接问题

java - Android:URLConnection.getContentLength() 给出不正确的值