【发布时间】:2015-05-24 20:03:23
【问题描述】:
我想获取网页的 HTML 代码,并将其显示在 edittext 控件中,但最终出现以下错误:
1482-1491/android.process.acore E/StrictMode﹕获得资源 在附加的堆栈跟踪中,但从未释放。请参阅 java.io.Closeable 了解 有关避免资源泄漏的信息。
这是我的代码:
class GetResult implements Runnable {
private volatile String bodyHtml;
@Override
public void run() {
try {
String myUri = "http://www.google.com";
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(myUri);
HttpResponse response = httpClient.execute(get);
bodyHtml = EntityUtils.toString(response.getEntity());
//return bodyHtml;
} catch (IOException e) {
bodyHtml = "kapot";
}
}
public String getbodyHtml(){
return bodyHtml;
}
}
和
String rs = "";
GetResult foo = new GetResult();
new Thread(foo).start();
rs = foo.getbodyHtml();
我做错了什么?
【问题讨论】:
-
我没有仔细研究你的代码,但只要寻找任何 Closable 对象,并在你完成它们后关闭它们。 (也许是它的 HttpResponse?)
-
关闭你的资源。
-
请注意,您的代码将不起作用,因为您在下载之前就调用了
getbodyHtml()。
标签: java android httpclient webpage