gpt4 book ai didi

java - JSON 解析后小部件未更新

转载 作者:行者123 更新时间:2023-12-01 13:49:09 25 4
gpt4 key购买 nike

我有以下代码:

@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"
}
]

最佳答案

将您的 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.
}
}

并将执行更改为new GetJSON().execute();

关于java - JSON 解析后小部件未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com