0

I am using following function to download some html from webserver. and then want to show that in webview.

private static final String request(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    HttpGet get = new HttpGet(url);
    try {
        response = httpclient.execute(get);
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        //TODO Handle problems..
    } catch (IOException e) {
        //TODO Handle problems..
    }
    return responseString;
}

I am using the function in an AsynTask. Here's the function which contains AsyncTask's code.

private void downloadInBackground(final String url, final WebView webview) {

    new AsyncTask<String, String, String>() {
        @Override
        protected String doInBackground(String... strings) {
            Log.d("asyncdebug", "sending!");
            String response = request(url);
            return response;
        }

        @Override
        protected void onPostExecute(String response) {
            Log.d("asyncdebug", "received!");
            webview.loadDataWithBaseURL("http://www.somedomainname.com/new/",response, "text/html", "UTF-8", null);
            super.onPostExecute(response);

        }


        @Override
        protected void onProgressUpdate(String... param) {
            Log.d("asyncdebug", "progressing... ");
        }


    }.execute("");
}

How can I get the update progress of the download process here?

Anik
  • 2,692
  • 2
  • 22
  • 25
  • Full example code.... http://stackoverflow.com/questions/11503791/progress-bar-completed-download-display-in-android – Dexter Oct 13 '14 at 14:58

1 Answers1

0

If you don't know the size of the data you are downloading you can use an indeterminate progress bar.

For example, you can put this into your AsyncTask onPreExecute:

        mProgressDialog = ProgressDialog.show(MyActivity.this, "", "Downloading...", true);
        mProgressDialog.setCancelable(false);

And then in your onPostExecute:

        mProgressDialog.dismiss();

More info here: Android ProgressDialog.

Gravitoid
  • 1,294
  • 1
  • 20
  • 20