2013-11-20 31 views
0

我有以下代码:的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],并通过[0]被设置为正确的值。您也可能需要更新您的用户界面才能看到更改。编辑:我在这里找到了答案http://stackoverflow.com/questions/4433464/settextviewtext-not-updating-widget – SpiderPig

+0

我应该调用'appWidgetManager.updateAppWidget(appWidgetId,views);'我在想,但它只能被调用从'onUpdate()'方法。 – Si8

+0

我在另一个答案中看到了那一行,但我应该在哪里添加它?在'onPostExecute'里面? – Si8

回答

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();

+0

不应该将'protected void doInBackground()'更改为'protected String doInBackground()'? – Si8

+0

仍然没有任何内容显示在窗口小部件中:/ – Si8

+0

是的,'protected String doInBackground()'。尝试调试并查看是否所有数据都正确解析。你需要更新主屏幕小部件吗? – yurezcv