【问题标题】:Progress dialog android json进度对话框android json
【发布时间】:2014-01-29 17:54:53
【问题描述】:

我的进度对话框有问题,我的应用程序从 json 中搜索数据,然后对话框出现,但此时我想要它,而他正在收集数据。我的错在哪里?

有编辑过的代码 logcat(致命异常:主要 java.lang.NullPointerException) JSON ([{"name":"Test"}]) 编辑:

public class MainActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    ProgressDialog pd;

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pd.dismiss();
        JSONObject jObject = result;
        try {
            String aJsonString = jObject.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, (List<String>) result));
        // parse and set List adapter here
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "dialog title",
                "dialog message", true);
    }

    @Override
    protected JSONObject doInBackground(Void... arg0) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("***");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

【问题讨论】:

    标签: android json progressdialog


    【解决方案1】:

    Thread.sleep(50);

    您正在阻止它的 ui 线程上调用 sleep。删除它。

    使用AsyncTask 而不是Thread

    http://developer.android.com/reference/android/os/AsyncTask.html

    onPreExecute 中显示进度对话框。在doInbackground 中执行您的http 请求。关闭onPostExecute 中的对话框并相应地更新用户界面。

    调用

     new TheTask().execute();
    

    然后

     class TheTask extends AsyncTask<Void, Void, Void>    {
    
            ProgressDialog pd;
    
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                progress.dismiss();
    
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                 progress = ProgressDialog.show(MainActivity.this, "dialog title",
                                "dialog message", true);
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                // http request
                return null;
            }   
        } 
    

    编辑:

    public class MainActivity extends ListActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        new TheTask().execute();
        }
    class TheTask extends AsyncTask<Void, Void, JSONObject> {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;
    
        ProgressDialog pd;
    
        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            pd.dismiss();
            // parse and set List adapter here
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = ProgressDialog.show(MainActivity.this, "dialog title",
                    "dialog message", true);
        }
    
        @Override
        protected JSONObject doInBackground(Void... arg0) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("******");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
    
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }
    
            // Convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
    
            try {
    
                jArray = new JSONObject(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
    
            return jArray;
        }
    }
    }
    

    编辑2:

       class TheTask extends AsyncTask<Void, Void, JSONArray> {
        InputStream is = null;
        String result = "";
        JSONArray jArray = null;
    
        ProgressDialog pd;
    
        @Override
        protected void onPostExecute(JSONArray result) {
            super.onPostExecute(result);
            pd.dismiss();
            ArrayList<String> list= new ArrayList<String>();
            try
            {
            for(int i=0;i<result.length();i++)
            {
    
                JSONObject jb = result.getJSONObject(i) ;
                String name = jb.getString("name");
                list.add(name);
            }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list));
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = ProgressDialog.show(MainActivity.this, "dialog title",
                    "dialog message", true);
        }
    
        @Override
        protected JSONArray doInBackground(Void... arg0) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("******");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
    
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }
    
            // Convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
    
            try {
    
                jArray = new JSONArray(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
    
            return jArray;
        }
    }
    }
    

    【讨论】:

    • 使 asynctask 成为活动类的内部类也可以让您更新代码
    • 我该怎么做?我是这个 android 编程的新手
    • 如果你懂 java,这应该很难。发布你的鳕鱼
    • @user3241084 看到编辑,你需要解析 json
    • pastebin.com/MEvZVMZE 那里我想解析 Json 但我的应用退出了
    【解决方案2】:

    您在UI Thread 上调用Thread.sleep(50),因此UI 将冻结直到完成。将您的操作移至AsyncTaskdoInBackground() 中的sleep()。将您所有的网络代码也放入doInBackground()

    您可以使用AsyncTasks其他方法更新UI

    See this answer for an example of AsyncTask如果你不熟悉

    AsyncTask Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多