2014-03-26 64 views
0

我正在從URL的JSON響應,並將其轉換成字符串。我想解析這個字符串以從響應中獲取一些值。但是,當解析發生時,應用程序會顯示一個空白屏幕(黑屏),直到解析響應。我想顯示一個ProgressDialog,它顯示要下載多少數據,以便應用程序不顯示空白屏幕。我試着顯示一個ProgressDialog,但它在解析之前和完成之後顯示。時間之間仍然顯示空白屏幕。顯示百分比進度對話框在解析JSON響應的Android

這裏是我的代碼: -

String registerContet = "myUrl"; 

    String items; 
    try 
    { 

     items = new FetchItems().execute(registerContet).get(); 

     pDialog = new ProgressDialog(this).show(Home.this, "Fetching news items", "Please wait.."); 

     JSONArray jObject = new JSONArray(items); 
     for (int i = 0; i < jObject.length(); i++) 
     { 
      JSONObject menuObject = jObject.getJSONObject(i); 

      String title= menuObject.getString("Title"); 
      String description= menuObject.getString("BodyText"); 
      String thumbnail= menuObject.getString("ThumbnailPath"); 
      String newsUrl = menuObject.getString("Url"); 
      String body = menuObject.getString("Body"); 
      String newsBigImage = menuObject.getString("ImageBlobUrls"); 

       map = new HashMap<String,String>(); 
       map.put(SOURCETITLE, title); 
       map.put(TITLE, description); 
       map.put(THUMBNAILPATH, thumbnail); 
       map.put(BODY, body); 
       map.put(URL, newsUrl); 
       map.put(IMAGEBLOBURLS,newsBImage); 

       myNList.add(map); 

    } 
     itemsAdapter = new LazyAdapter(Home.this, myNList); 
     if(pDialog!=null && pDialog.isShowing()) 
     { 
      pDialog.dismiss(); 
     } 
     nList.setAdapter(itemsAdapter); 

     nList.setOnItemClickListener(new OnItemClickListener() 
     { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, 
        View arg1, int position, long arg3) 
      { 
       // TODO Auto-generated method stub 
       myDialog = new ProgressDialog(Home.this).show(Home.this, "Fetching news..", "Just a moment"); 


       HashMap<String, String> myMap = myNList.get(position); 
       Intent nIntent = new Intent(Home.this,NDetails.class); 
       newsIntent.putExtra("NItems", myMap); 

       startActivity(nIntent); 

      } 
     }); 
    } 
    catch (InterruptedException e) { 

     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



} 

FetchItems.java

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

    @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(myDialog.isShowing()) { myDialog.dismiss(); } 
     */ 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     /* 
     * myDialog = new ProgressDialog(Home.this); 
     * myDialog.setMessage("Loading"); myDialog.show(); 
     */ 
    } 

} 

誰能告訴我怎樣才能解決這個問題。 感謝

+0

添加對話框中顯示的代碼在你的preexecute方法,然後在你的onPostExecute方法添加解析代碼。 – GrIsHu

回答

0

在onPreExecute方法使用此代碼,

private ProgressDialog dialog; 


dialog = new ProgressDialog(this); 
      dialog.setMessage("Please Wait..."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(false); 
      dialog.show(); 
+0

我曾嘗試已經 – user3354605

+0

請告訴我問題?.. – Beginner

+0

它的對話框顯示,然後黑屏仍顯示 – user3354605

1
public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 

在活動創建對話框:

@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Converting.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
     } 
    } 

顯示對話框中onPreExecute()

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

派息小姐對話框中onPostExecute()

@Override 
    protected void onPostExecute(String unused) { 
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
+0

這如何顯示百分比進度? – user3354605

0

容易和簡單的代碼中的進度對話框百分比對話框

 > protected void onPreExecute() { 

     dialog = new ProgressDialog(UploadActivity.this); 
     dialog.setMessage("Loading, please wait.. "); 
     dialog.show(); 
     dialog.setCancelable(false); 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 

      dialog.setMessage("Loading, please wait.. "+String.valueOf(progress[0])+"%"); 

    }