Android:AsyncTask,如何更新ProgressDialog增量
我想分析一个网页,并将progressdialog样式水平显示,然后逐字节递增,这是可能的?试试这样的方法 创建进度对话框。Android:AsyncTask,如何更新ProgressDialog增量,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我想分析一个网页,并将progressdialog样式水平显示,然后逐字节递增,这是可能的?试试这样的方法 创建进度对话框。 ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this); mProgressDialog.setMessage("Here you can set a message"); mProgressDialog.setIndeterminate(false); mProgressDialog
ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this);
mProgressDialog.setMessage("Here you can set a message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
MyAsyncTask obj = new MyAsyncTask ();
obj.execute("url");
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int length = connection.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/length));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
public void onProgressUpdate(String... args){
mProgressDialog.setProgress(args[0]);
}
}
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您的AsyncTask类。
ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this);
mProgressDialog.setMessage("Here you can set a message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
MyAsyncTask obj = new MyAsyncTask ();
obj.execute("url");
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int length = connection.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/length));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
public void onProgressUpdate(String... args){
mProgressDialog.setProgress(args[0]);
}
}
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…url){
整数计数;
试一试{
URL=新URL(URL[0]);
URLConnection=url.openConnection();
connection.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int length=connection.getContentLength();
//下载文件
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(“/sdcard/file_name.txt”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
出版进度(整数)(总计*100/长度);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
@凌驾
public void onProgressUpdate(字符串…参数){
mProgressDialog.setProgress(参数[0]);
}
}
}
您必须在AndroidManifest文件中授予这些权限。
ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this);
mProgressDialog.setMessage("Here you can set a message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
MyAsyncTask obj = new MyAsyncTask ();
obj.execute("url");
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int length = connection.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/length));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
public void onProgressUpdate(String... args){
mProgressDialog.setProgress(args[0]);
}
}
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
是的,可以参考此示例链接:谢谢您的回答。如果我想从mysql数据库的url和php页面导入5000条记录,我可以用字节计算JSONArray吗?在这种情况下,如何应用此代码?感谢这个函数:-getJSONArray公共JSONArray getJSONArray(int index)抛出JSONException获取与索引关联的JSONArray。参数:index-索引必须介于0和length()之间-1。返回:一个JSONArray值。抛出:JSONException-如果索引没有值。或者,如果该值不是JSONArray,并且最后一次使用此代码,但使用此代码我可以知道JSONArray维度(以字节为单位)?在某些URL中,您将始终得到-1,然后使用此代码HttpURLConnection=(HttpURLConnection)URL.openConnection();连接。设置请求方法(“HEAD”);connection.connect();int length=connection.getContentLength();