How can I get the process percentage of a HTTP request?
I’ve got this code for the request:
HttpClient requestclient = new DefaultHttpClient();
HttpResponse requestresponse = requestclient.execute(new HttpGet("example.com"));
StatusLine requeststatus = requestresponse.getStatusLine();
if(requeststatus.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream requestoutput = new ByteArrayOutputStream();
requestresponse.getEntity().writeTo(requestoutput);
requestoutput.close();
String requestresult = requestoutput.toString();
return requestresult;
} else{.
requestresponse.getEntity().getContent().close();
throw new IOException(requeststatus.getReasonPhrase());
}
Thanks!
HTTP requests should be run either in an IntentService or AsyncTask. That is, always run them in the background.
AsyncTask has methods for displaying the percentage complete: publishProgress() from doInBackground(), and onProgressUpdate() in the main Activity.
An IntentService can show progress by broadcast Intents back to the Activity, or by running a progressBar, or (in JellyBean and later) by using a progress notification.