简体   繁体   English

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

[英]Showing Progress Dialog while loading data from the internet

I want to show a Progress Dialog on button click in my app while data is loaded from the internet. 我想在从互联网上加载数据时在我的应用程序中单击按钮时显示进度对话框。 I can't get it to work, could someone give me some tips on where to put the Dialog function? 我无法使其正常工作,有人可以给我一些有关将Dialog函数放在何处的提示吗?

This is my AsyncTask method: 这是我的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!");
        }
    }

In the AsyncTask class use onPrexecute method to display progress dialog and use onPostExecute to dismiss it: 在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();  
    }

Don't forget to define pDialog before you call it: 在调用它之前,请不要忘记定义pDialog:

 ProgresDialog pDialog;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM