我有一個應用程序,我從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看看,並給出答案。 謝謝
請給其具有所有這方面的一個樣品示例。這樣我就可以輕鬆搞定。 – Jyosna 2011-06-13 09:41:05
這是你的同一個問題,接受一個很好的源代碼示例: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a- progressdialog – Egor 2011-06-13 09:44:40