JSON解析后Widget没有更新

时间:2013-11-20 15:33:04

标签: 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_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"
    }
]

1 个答案:

答案 0 :(得分:1)

将您的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();