我正在尝试开发一个通过 json 资源每分钟更新一次的小部件,我的问题是当我尝试在 jellybean 上启动它时它会崩溃,而姜饼可以工作。
我在这里看到我必须将所有互联网连接移动到主线程,对吗?目前我有一个名为 HttpRequest 的类:
public class HttpRequest {
private String url;
public HttpRequest(String url)
{
this.url = url;
}
public String GetContent() throws ClientProtocolException, IOException
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
return html;
}
}
每分钟都被称为 updateAppWidget() ,它是 WidgetProvider 类的一个方法。在 updateAppWidget() 里面有:
HttpRequest r = new HttpRequest("http://www.hwlogos.com/test.json");
remoteViews.setTextViewText(R.id.ip, r.GetContent());
你能告诉我如何陡峭地解决它吗?谢谢