I want to parse a webpage and visual a progressdialog style horizontal and increment it byte to byte, it's possibile ?
Asked
Active
Viewed 1.5k times
6
-
Yes, it is possible http://goo.gl/eeiu5 – Matthieu Sep 28 '11 at 22:49
-
Refer this example link: https://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask – Uttam Sep 29 '11 at 04:40
-
Thank you for your answer. And if I want to import from url and php page 5000 records from mysql db, can I calculate the JSONArray in bytes ? How can I apply this code in that case ? Thanks – MimmoG Sep 29 '11 at 07:25
-
Try to this function :- getJSONArray public JSONArray getJSONArray(int index) throws JSONException Get the JSONArray associated with an index. Parameters: index - The index must be between 0 and length() - 1. Returns: A JSONArray value. Throws: JSONException - If there is no value for the index. or if the value is not a JSONArray and last follow this [link](http://www.json.org/javadoc/org/json/JSONArray.html) – Uttam Sep 29 '11 at 08:41
-
but with this code I can to know the JSONArray dimension in bytes ? – MimmoG Sep 29 '11 at 15:52
1 Answers
20
Try something like this,
Create a ProgressDialog.
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");
Your AsyncTask Class.
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]);
}
}
}
You have to give these Permission's in the AndroidManifest file.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Lalit Poptani
- 67,150
- 23
- 161
- 242
-
In some URL you will always get -1 ,then use this code HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("HEAD"); connection.connect(); int length = connection.getContentLength(); – Parag Chauhan Apr 21 '13 at 06:08