2011-06-13 213 views
0

我有一個應用程序,我從http服務器&設置數據的數據活動。 從服務器獲取數據需要時間。所以我想在那裏提供一個進度對話框。 我這樣做,但問題是progressdialog即將到來,但它不旋轉(只是常數)。 Plz有人告訴我它是怎麼來的!與進度對話框的Android問題

下面是我的代碼,

private void setupViews() 
    { 
     imgRestro = (ImageView) findViewById(R.id.imageResturant); 
     tvRestroName = (TextView) findViewById(R.id.tvRestroName); 
     tvRestroAddr = (TextView) findViewById(R.id.tvRestroAddr); 
     tvRestroArea = (TextView) findViewById(R.id.tvRestroArea); 
     tvRestroCity = (TextView) findViewById(R.id.tvRestroCity); 
     tvRestroPhone = (TextView) findViewById(R.id.tvRestroPhone); 
     tvRestroRating = (TextView) findViewById(R.id.tvRestroRating); 
     tvRestroCuisines = (TextView) findViewById(R.id.tvRestroCuisines); 
     tvRestroAttr = (TextView) findViewById(R.id.tvRestroAttributes); 
    } 
//to execute url 
    private String executeHttpGet(String URL) throws Exception { 

     URL url = null; 
     StringBuffer strBuf = null; 
     BufferedReader read = null; 
     HttpURLConnection httprequest = null; 
     try { 
      url = new URL(URL); 
      httprequest = (HttpURLConnection) url.openConnection(); 
      httprequest.setRequestMethod("GET"); 
      httprequest.setDoInput(true); 
      httprequest.setDoOutput(true); 
      httprequest.setConnectTimeout(5000); //time for before connect 
      httprequest.setRequestProperty("Content-Type", 
        "text/plain; charset=utf-8"); 
      //httprequest.setReadTimeout(15000); //time for after connect 

      read = new BufferedReader(new InputStreamReader(
        httprequest.getInputStream())); 

      strBuf = new StringBuffer(); 

      String line = ""; 

      while ((line = read.readLine()) != null) { 
       strBuf.append(line).append("\n"); 
      } 

     } catch (IOException ex) { 

     } finally { 
      try { 
       httprequest.disconnect(); 
       read.close(); 
      } catch (Exception ex) { 

      } 
     } 
     return strBuf.toString(); 

    } 
    //to assign all the info from json to activity textview or imageview 
    private void examineJSONFile() { 
     try { 

      String str = ""; 
      page = executeHttpGet(url); 
      Log.i("hello", str.toString()); 

      JSONObject topobj = new JSONObject(page); 
      JSONObject innerobj = topobj.getJSONObject("restarutant"); 

      tvRestroName.setText("Restaurant "+innerobj.getString("name")); 
      String photoURL = innerobj.getString("photo"); 
      URI uri = new URI(photoURL); 
      Bitmap bmp = loadBitmap(photoURL); 
      System.out.println("str ;" + photoURL + " uri " + uri); 

      imgRestro.setImageBitmap(Bitmap.createScaledBitmap(bmp, 300, 300,true)); 
      tvRestroAddr.setText("Address: " + innerobj.getString("address")); 
      tvRestroArea.setText("Area: " + innerobj.getString("area")); 
      tvRestroCity.setText("City: " + innerobj.getString("city")); 
      JSONArray location = innerobj.getJSONArray("location"); 
      String lat = location.get(0).toString(); 
      String lng = location.get(1).toString(); 
      latitude = Double.valueOf(lat.trim()).doubleValue(); 
      longitude = Double.valueOf(lng.trim()).doubleValue(); 
      System.out.println("Lat :" + latitude + " Long :" + longitude); 

      JSONArray phone = innerobj.getJSONArray("phone"); 
      tvRestroPhone.setText("Phone: " + phone.get(0).toString() + " ," 
        + phone.get(1).toString()); 

      tvRestroRating.setText("Rating: " + innerobj.getString("rating")); 

      JSONArray cuisines = innerobj.getJSONArray("cuisines"); 
      tvRestroCuisines.setText("Cuisines: " + cuisines.get(0).toString() 
        + " ," + cuisines.get(1).toString()); 

      JSONArray attributes = innerobj.getJSONArray("attributes"); 
      tvRestroAttr.setText("Attributes: " + attributes.get(0).toString() 
        + " ," + attributes.get(1).toString() + " ," 
        + attributes.get(2).toString()); 

      Log.i("hello", str.toString()); 

     } catch (Exception je) { 
      tvRestroAddr.setText("Error w/file: " + je.getMessage()); 
     } 
    } 

    // to get image from url in form of bitmap 
    private static Bitmap loadBitmap(String url) { 
     URL myFileUrl = null; 
     InputStream is = null; 
     try { 
      myFileUrl = new URL(url); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl 
        .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      is = conn.getInputStream(); 
      Log.i("im connected", "Download"); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return BitmapFactory.decodeStream(is); 
    } 

    //for progressdialog 
    private class DownloadTask extends AsyncTask<Void, Void, Void> { 
      private ProgressDialog dialog = new ProgressDialog(InfoActivity.this); 

      protected void onPreExecute() { 
       System.out.println("In onPreExecute "); 
       dialog.setTitle("Loading.."); 
       dialog.setMessage("Downloading source.."); 
       dialog.setProgress(100); 
       dialog.setMax(5000); 
       dialog.show(); 
      } 

      protected Void doInBackground(Void... unused) { 
       //examineJSONFile(); 
       System.out.println("In doInBackground "); 
       return null; 
      } 


      protected void onPostExecute(Void unused) { 
       System.out.println("In onPostExecute ");  
        dialog.dismiss(); 
         examineJSONFile();   
} 

PLZ看看,並給出答案。 謝謝

回答

1

您應該覆蓋AsyncTask中的onProgressUpdate()方法並在那裏更改對話框的值。您可以通過方法從doInBackground()將您的進度值發送到對話框。希望這可以幫助。

+0

請給其具有所有這方面的一個樣品示例。這樣我就可以輕鬆搞定。 – Jyosna 2011-06-13 09:41:05

+0

這是你的同一個問題,接受一個很好的源代碼示例: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a- progressdialog – Egor 2011-06-13 09:44:40

0

嘗試使用異步任務,如下所示:

try{ 
    class test extends AsyncTask{ 


TextView tv_per; 
int mprogress; 

Dialog UpdateDialog = new Dialog(ClassContext); 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    mprogress = 0; 

    UpdateDialog.setTitle(getResources().getString(R.string.app_name)); 
    UpdateDialog.setContentView(R.layout.horizontalprogressdialog); 
    TextView dialog_message = (TextView)UpdateDialog.findViewById(R.id.titleTvLeft); 
    tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage); 
    dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data)); 
    dialog_message.setGravity(Gravity.RIGHT); 
    UpdateDialog.setCancelable(false); 
    UpdateDialog.show(); 
    super.onPreExecute(); 
} 



@Override 
protected void onProgressUpdate(Object... values) { 
    // TODO Auto-generated method stub 
    ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar); 
    update.setProgress((Integer) values[0]); 
    int percent = (Integer) values[0]; 
if(percent>=100) 
    { 
     percent=100; 
    } 
    tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage); 
    tv_per.setText(""+percent); 
} 



@Override 
protected Object doInBackground(Object... params) { 
    // TODO Auto-generated method stub 
    //your code 
} 

    super.onPostExecute(result); 
    UpdateDialog.dismiss(); 
} 

} 
new test().execute(null); 

} 
catch(Exception e) 
{ 
e.printStackTrace(); 
}