窗口小部件JSON解析后没有更新 [英] Widget is not updating after JSON parse
本文介绍了窗口小部件JSON解析后没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code:
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
super.onReceive(背景下,意图);
如果(CLOCK_WIDGET_UPDATE.equals(intent.getAction())){
Toast.makeText(上下文,onReceiver(),Toast.LENGTH_LONG).show();
}
新的getJSON()执行(NULL,NULL,NULL);
}
公共类的getJSON扩展的AsyncTask<虚空,虚空,虚空> {
@覆盖
保护无效doInBackground(虚空...... PARAMS){//在后台运行
尝试 {
HttpClient的=新DefaultHttpClient(新BasicHttpParams());
HttpPost httppost =新HttpPost(http://pagesbyz.com/hadith.json);
//取决于Web服务
httppost.setHeader(内容型,应用/ JSON);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
的InputStream = entity.getContent();
// JSON是UTF-8在默认情况下
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(InputStream中,UTF-8),8);
SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(行+\ N);
}
结果= sb.toString();
}赶上(例外五){
Log.i(TEST,e.toString());
//哎呀
}
最后 {
尝试{如果(的InputStream!= NULL)inputStream.close();}赶上(例外挤){}
}
返回null;
}
@覆盖
在preExecute()保护无效{//活动的进展情况
}
@覆盖
保护无效onPostExecute(空V){
尝试 {
jsonArray =新JSONArray(结果);
日期=新的String [jsonArray.length()];
报价=新的String [jsonArray.length()];
通过=新的String [jsonArray.length()];
的for(int i = 0; I< jsonArray.length();我++){
JSONObject的jsonObj =(的JSONObject)jsonArray.get(我);
日期[i] = jsonObj.getString(日期);
报价[I] = jsonObj.getString(报价);
由[i] = jsonObj.getString(通过);
} //结束循环
views.setTextViewText(R.id.tvToday,日期[0]);
views.setTextViewText(R.id.tvParkStatus,报价[0]);
}
赶上(JSONException E){
e.printStackTrace();
}
}
}
我期待更新从JSON文件接收到的信息的部件。我看小部件,但并未显示信息。
我的JSON是这样的:
[
{
日期:11182013,
报价:今天是星期一,
本经:SiKni8
},
{
日期:11192013,
报价:今天是星期二,
本经:SiKni8
}
]
解决方案
更改AsynkTast为:
公共类的getJSON扩展的AsyncTask<虚空,虚空,字符串> {
@覆盖
保护无效doInBackground(虚空...... PARAMS){//在后台运行
// ...
//做所有的东西,因为它是和更改回这样的:
返回结果;
}
@覆盖
保护无效onPostExecute(字符串结果){
//留给其他的你code,因为它是。
}
}
和变更执行,以新的getJSON()执行();
I have the following code:
@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();
}
}
}
I am looking to update the widget with the information received from the JSON file. I see the widget but no information is being displayed.
My JSON looks like:
[
{
"Date" : "11182013",
"Quote" : "Today Is Monday",
"By" : "SiKni8"
},
{
"Date" : "11192013",
"Quote" : "Today Is Tuesday",
"By" : "SiKni8"
}
]
解决方案
Change your AsynkTast to:
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.
}
}
And change execution to new GetJSON().execute();
这篇关于窗口小部件JSON解析后没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文