【发布时间】:2015-05-25 11:19:11
【问题描述】:
我正在尝试在 Android 中获取带有 HttpClient() 的网页的 HTML 代码。 getbodyHtml 返回空,而在输出中我看到 HTML 代码打印得很好。我做错了什么?
class GetResult implements Runnable {
public volatile String bodyHtml;
public volatile boolean finished = false;
@Override
public void run() {
finished = false;
HttpClient httpClient = new DefaultHttpClient();
try {
String myUri = "http://google.com";
HttpGet get = new HttpGet(myUri);
HttpResponse response = httpClient.execute(get);
bodyHtml = EntityUtils.toString(response.getEntity());
//return bodyHtml;
finished = true;
System.out.println(bodyHtml);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public String getbodyHtml(){
return bodyHtml;
}
}
还有这个:
String rs = "";
GetResult foo = new GetResult();
new Thread(foo).start();
if (foo.finished = true){
rs = foo.getbodyHtml();
}
edittext2.setText(rs);
【问题讨论】:
-
if (foo.finished = true){但事实并非如此。
标签: java android httpclient