【问题标题】:How to know progress in Android HttpClient?如何知道 Android HttpClient 的进度?
【发布时间】:2014-10-13 11:30:55
【问题描述】:

我正在使用以下功能从网络服务器下载一些 html。然后想在 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;
}

我在AsynTask 中使用该函数。这是包含 AsyncTask 代码的函数。

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("");
}

如何在此处获取下载过程的更新进度?

【问题讨论】:

标签: android httpwebrequest androidhttpclient


【解决方案1】:

如果您不知道要下载的数据的大小,可以使用不确定的进度条。

例如,您可以将其放入您的 AsyncTask onPreExecute:

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

然后在你的 onPostExecute 中:

        mProgressDialog.dismiss();

更多信息在这里:Android ProgressDialog

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多