2014-06-26 78 views
-3

我有兩個AsyncTasks。在第一個Iam中,以JSON形式從服務器獲取數據並將其轉換爲字符串。在第二個Iam解析字符串到對象中。使用AsynTAsk解析JSON並填充列表時顯示ProgressDialog

這種情況發生在用戶點擊AlertDialog中的一個按鈕之後。當發生這種情況時,我想顯示一個ProgressDialog。但是現在ALertDialog在點擊按鈕後解散了,這很好。但進度對話框不顯示。

這裏是我的代碼: -

private void PostString(String postedString) 
      { 
       // TODO Auto-generated method stub 

       String postUrl = EndPoints.PostURL; 
       try 
       { 
        String Response = new Post().execute(postUrl).get(); 
        String getRequesturl= url 
        String items = new FetchItems().execute(getRequesturl).get(); 
        ArrayList<HashMap<String, String>> updatedPostList = new GetList().execute(items).get(); 

}

private class Post extends AsyncTask<String, String, String> 
    { 




     @Override 
     protected String doInBackground(String... params) 
     { 
      // TODO Auto-generated method stub 

      HttpResponse response =null; 
      String resultString = ""; 
      String myResponseBody = "" ; 
      // Creating HTTP client 
      HttpClient httpClient = new DefaultHttpClient(); 
      // Creating HTTP Post 
      HttpPost request = new HttpPost(params[0]); 

      List<NameValuePair> nameValuePair =nameValuePairs 


      try 
      { 
       request.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
       response = httpClient.execute(request); 
       if(response.getStatusLine().getStatusCode()== 200) 
       { 
        HttpEntity entity = response.getEntity(); 
        if (entity != null) 
        { 

         InputStream inputStream = entity.getContent(); 
         myResponseBody = convertToString(inputStream); 
        } 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return myResponseBody; 
     } 


private class FetchItems extends AsyncTask<String, String, String> 
    { 
     // TODO Auto-generated method stub 

     @Override 
     protected String doInBackground(String... params) 
     { 
      // TODO Auto-generated method stub 
      HttpResponse response =null; 
      String resultString = ""; 
      String myResponseBody = "" ; 
      // Creating HTTP client 
      HttpClient httpClient = new DefaultHttpClient(); 
      // Creating HTTP Post 
      HttpGet request = new HttpGet(params[0]); 


      try 
      { 
       response = httpClient.execute(request); 
       if(response.getStatusLine().getStatusCode()== 200) 
       { 
        HttpEntity entity = response.getEntity(); 
        if (entity != null) 
        { 

         InputStream inputStream = entity.getContent(); 
         myResponseBody = convertToString(inputStream); 
        } 
       } 
      } 
      catch(Exception e) 
      { 
      } 
      return myResponseBody; 
     } 





     @Override 
     protected void onPostExecute(String result) 
     { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

      if(pd!=null && pd.isShowing()) 
       pd.dismiss(); 
     } 





     @Override 
     protected void onPreExecute() 
     { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 


     } 


private class GetList extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> 
    { 


     @Override 
     protected ArrayList<HashMap<String, String>> doInBackground(String... params) 
     { 
      // TODO Auto-generated method stub 
      myCList.clear(); 
      try { 
       JSONObject jsonResponse = new JSONObject(new String(params[0])); 
       JSONArray mtUsers = jsonResponse.getJSONArray("ListOfC"); 
       for (int i = 0; i < mtUsers.length(); i++) 
       { 
        JSONObject menuObject = mtUsers.getJSONObject(i); 
//Parsing 

        map = new HashMap<String,String>(); 
        map.put(My items here) 

        myCommentsList.add(map); 

       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Collections.reverse(myList); 
      return myList; 
     } 



    } 



case R.id.btnAdd: 
      scrollNews.fullScroll(v.FOCUS_DOWN); 
      btnAddComms.setPressed(true); 

      builder = new AlertDialog.Builder(NewsDetails.this); 
      builder.setTitle("Post Comment"); 
      builder.setIcon(R.drawable.post_comment_button); 


      final EditText input1 = new EditText(NewsDetails.this); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.MATCH_PARENT); 

      input1.setLayoutParams(lp); 
      builder.setView(input1); 


      builder.setPositiveButton("Post", new DialogInterface.OnClickListener() 
      { 

       public void onClick(DialogInterface dialog, int id) 
       { 

        pd = new ProgressDialog(Details.this); 
        pd.setMessage("Posting.."); 
        pd.show(); 
        pd.setCancelable(true); 

        postedString= input1.getText().toString(); 
        dialog.dismiss(); 

        if(postedString.length()>0) 
        { 
         Post(postedString); 

        } 
        else 
        { 
         Toast.makeText(Details.this, "Please enter a text.", Toast.LENGTH_LONG).show(); 
         input1.findFocus(); 
        } 





       } 

回答

0

創建ProgressDialog作爲一個實例變量。

pDialog = new ProgressDialog(this); 
pDialog.setMessage("Fetching data"); 

內onPreExecute()的AsyncTask的方法:

pDialog.show(); 

內onPostExecute()AsnycTask的方法:

pDialog.dismiss(); 

注1:你不需要多AsyncTasks,你可以因爲您在一個之後調用一個AsyncTask,因此只需完成第一個AsyncTask中的整個提取和解析。注意2:當您在UI線程上執行asyncTask.execute.get()時,您的UI將被阻止,這不被鼓勵。所以,只需調用asyncTask.execute()並在後臺發生一切時顯示進度對話框。

+0

Iam在執行execute.get()來獲取一個字符串和一個列表。我該如何從任務本身做到這一點? – user3713706

+0

並且當您等待結果時,您的用戶界面是否無響應? –

+0

另外,我想你可能希望在執行所有異步任務時顯示進度對話框,所以只需將pDialog.dismiss()部分放入最後一個AsyncTask onPostExecute()方法內。 –