Java JSON解析后小部件未更新
我有以下代码:Java JSON解析后小部件未更新,java,android,json,Java,Android,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_LO
@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();
}
}
}
将您的AsynkTast更改为:
public class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected Void doInBackground(Void... params) { //Running in background
// ...
// do all your stuff as it is and change return to this :
return result;
}
@Override
protected void onPostExecute(String result) {
// left other of your code as it is.
}
}
public类GetJSON扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…params){//在后台运行
// ...
//按原样完成所有工作,然后更改并返回:
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//让其他代码保持原样。
}
}
并将执行更改为
newgetjson().execute()代码>检查日期[0]、引号[0]和日期[0]是否设置为正确的值。此外,您可能需要更新UI以查看更改。编辑:我在这里找到了一个答案,我应该调用appWidgetManager.UpdateAppWidgetId(appWidgetId,视图)
我正在思考,但只能从onUpdate()
方法调用它。我在另一个答案中看到了这一行,但我应该在哪里添加它?在onPostExecute
?我是否应该将protectedvoid doInBackground()
更改为protected String doInBackground()
?小部件中仍然没有显示任何内容:/Yes,protected String doInBackground()
。尝试调试并查看是否正确解析了所有数据。您需要更新主屏幕小部件吗?主屏幕小部件没有显示任何内容。。。两个文本视图。Eclipse调试器甚至没有启动:/
public class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected Void doInBackground(Void... params) { //Running in background
// ...
// do all your stuff as it is and change return to this :
return result;
}
@Override
protected void onPostExecute(String result) {
// left other of your code as it is.
}
}