【发布时间】: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("");
}
如何在此处获取下载过程的更新进度?
【问题讨论】:
-
完整示例代码....stackoverflow.com/questions/11503791/…
标签: android httpwebrequest androidhttpclient