我正在尝试创建一个应用程序,它将从网址下载网页的源代码并在 TextView 中显示。我还希望它显示水平进度条< / strong>在后台下载页面时。我已成功使用 Asynctask 在后台下载网页,但我不知道如何找出网页的大小并根据后台的下载状态更新进度条
这就是我正在下载的网页源代码 -
package com.example.gc;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.cd);
grabURL("http://google.com");
}
public void grabURL(String url) {
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
if (Error != null) {
Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG).show();
} else {
// Toast.makeText(MainActivity.this, "Source: " + Content, Toast.LENGTH_LONG).show();
tv.setText(Content);
}
}
}
}
请帮助我在我的应用中显示和更新水平进度条。提前谢谢!
答案 0 :(得分:1)
您只想在AsyncTask onPreExecute()中创建进度对话框。如下所示:
ProgressDialog mProgressDialog;
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getParent());
mProgressDialog.setTitle("Please wait");
mProgressDialog.setMessage("Downloading ...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
//Downloading progress update
//your downloding logic over here
/* int lenghtOfFile= urlConnection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = newFileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));*/
}
} catch (Exception e) {}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
//Update Progress
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}
}
在这里,我将向您展示如何将水平进度对话框实现为AsyncTask.Now,并按您自己的方式进行自定义。
答案 1 :(得分:0)
答案 2 :(得分:-1)
private class GrabURL extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... arg0) {
// download page
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}