java - 从 Internet 加载数据时显示进度对话框

标签 java android dialog android-asynctask

我想在从 Internet 加载数据时在我的应用程序中单击按钮时显示进度对话框。我无法让它工作,有人可以给我一些关于在哪里放置对话框功能的提示吗?

这是我的 AsyncTask 方法:

private class GetTweets extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... twitterURL) {
            //start building result which will be json string
            StringBuilder tweetFeedBuilder = new StringBuilder();
            //should only be one URL, receives array
            for (String searchURL : twitterURL) {
                HttpClient tweetClient = new DefaultHttpClient();
                try {
                    //pass search URL string to fetch
                    HttpGet tweetGet = new HttpGet(searchURL);
                    //execute request
                    HttpResponse tweetResponse = tweetClient.execute(tweetGet);
                                            StatusLine searchStatus = tweetResponse.getStatusLine();
                    if (searchStatus.getStatusCode() == 200) {
                        //get the response
                        HttpEntity tweetEntity = tweetResponse.getEntity();
                        InputStream tweetContent = tweetEntity.getContent();
                                                    InputStreamReader tweetInput = new InputStreamReader(tweetContent);
                        BufferedReader tweetReader = new BufferedReader(tweetInput);
                        String lineIn;
                        while ((lineIn = tweetReader.readLine()) != null) {
                            tweetFeedBuilder.append(lineIn);
                        }
                    }
                    else
                        tweetDisplay.setText("Error!");
                }
                catch(Exception e){ 
                    tweetDisplay.setText("Error!");
                    e.printStackTrace(); 
                }
            }
            //return result string
            return tweetFeedBuilder.toString();
        }
                    protected void onPostExecute(String result) {
            //start preparing result string for display
            StringBuilder tweetResultBuilder = new StringBuilder();
            try {
                //get JSONObject from result
                JSONObject resultObject = new JSONObject(result);
                //get JSONArray contained within the JSONObject retrieved - "results"
                JSONArray tweetArray = resultObject.getJSONArray("results");
                //loop through each item in the tweet array
                for (int t=0; t<tweetArray.length(); t++) {
                    //each item is a JSONObject
                    JSONObject tweetObject = tweetArray.getJSONObject(t);
                                        tweetResultBuilder.append(tweetObject.getString("from_user")+": ");
                    tweetResultBuilder.append(tweetObject.get("text")+"\n\n");
                }
            }
            catch (Exception e) {
                tweetDisplay.setText("Error!");
                e.printStackTrace();
            }
            //check result exists
            if(tweetResultBuilder.length()>0)
                tweetDisplay.setText(tweetResultBuilder.toString());
            else
                tweetDisplay.setText("no results!");
        }
    }

最佳答案

在 AsyncTask 类中使用 onPrexecute 方法显示进度对话框并使用 onPostExecute 关闭它:

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(YOUR_ACTIVITY_CLASS_NAME.this);
        pDialog.setMessage("Please Wait");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected void onPostExecute(String str)
    {
        // Dismiss the dialog once finished
        pDialog.dismiss();  
    }

不要忘记在调用之前定义 pDialog:

 ProgresDialog pDialog;

关于java - 从 Internet 加载数据时显示进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648420/

相关文章:

javascript - 发送对话框在页面选项卡中不起作用

java - Android loopj Async Http 在 1.4.5 更新后崩溃

java - 我可以切换 JPanel 数组中 JPanel 的位置吗?

java - setOnClickListener(this) 的性能注意事项

java - 通过数据库进行进程间通信

android - 使用Android和Phonegap存储SQLite数据库

java - 如何在 Android 项目构建路径上使用需要 1.7 合规级别的 Java 项目?

android - 从 "showDialog"调用 "onResume"抛出运行时异常

java - 为什么有些 Android 事件停止传播而其他事件不需要?

软键盘打开时 Android 对话框布局挤压