我有以下代码:的Widget JSON解析后没有更新
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
new GetJSON().execute(null, null, null);
}
public class GetJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) { //Running in background
try {
httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://pagesbyz.com/hadith.json");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("TEST", e.toString());
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return null;
}
@Override
protected void onPreExecute() { //Activity is on progress
}
@Override
protected void onPostExecute(Void v) {
try {
jsonArray = new JSONArray(result);
date = new String[jsonArray.length()];
quote = new String[jsonArray.length()];
by = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = (JSONObject) jsonArray.get(i);
date[i] = jsonObj.getString("Date");
quote[i] = jsonObj.getString("Quote");
by[i] = jsonObj.getString("By");
} // End the for loop
views.setTextViewText(R.id.tvToday, date[0]);
views.setTextViewText(R.id.tvParkStatus, quote[0]);
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
我期待更新从JSON文件中收到的信息的部件。我看到小部件,但没有显示任何信息。
我的JSON的样子:
[
{
"Date" : "11182013",
"Quote" : "Today Is Monday",
"By" : "SiKni8"
},
{
"Date" : "11192013",
"Quote" : "Today Is Tuesday",
"By" : "SiKni8"
}
]
检查日期[0],报价[0],并通过[0]被设置为正确的值。您也可能需要更新您的用户界面才能看到更改。编辑:我在这里找到了答案http://stackoverflow.com/questions/4433464/settextviewtext-not-updating-widget – SpiderPig
我应该调用'appWidgetManager.updateAppWidget(appWidgetId,views);'我在想,但它只能被调用从'onUpdate()'方法。 – Si8
我在另一个答案中看到了那一行,但我应该在哪里添加它?在'onPostExecute'里面? – Si8