2011-06-13 98 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(); 
}