2013-12-11 66 views
2

我有3個textviews,我試圖更新數據通過JSON拉 - 我沒有得到任何錯誤,我迄今爲止已建 - 但textviews(nameTv,contentTv和publishedTv )似乎沒有更新我的JSON查詢中的數據。JSON查詢不會更新TextView

public class Player extends YouTubeBaseActivity implements 
     YouTubePlayer.OnInitializedListener { 

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXX"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.player); 
     String title = getIntent().getStringExtra("title"); 
     String uploader = getIntent().getStringExtra("uploader"); 
     String viewCount = getIntent().getStringExtra("viewCount"); 
     TextView titleTv = (TextView) findViewById(R.id.titleTv); 
     TextView uploaderTv = (TextView) findViewById(R.id.uploaderTv); 
     TextView viewCountTv = (TextView) findViewById(R.id.viewCountTv); 
     titleTv.setText(title); 
     uploaderTv.setText("by" + uploader + " |"); 
     viewCountTv.setText(viewCount + " views"); 
     YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview); 
     youTubePlayerView.initialize(API_KEY, this); 
     GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(null, viewCount); 
     task.execute(); 
    } 

    @Override 
    public void onInitializationFailure(Provider provider, 
      YouTubeInitializationResult result) { 
     Toast.makeText(getApplicationContext(), "onInitializationFailure()", 
       Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onInitializationSuccess(Provider provider, 
      YouTubePlayer player, boolean wasRestored) { 
     if (!wasRestored) { 
      String video_id = getIntent().getStringExtra("id"); 
      player.loadVideo(video_id); 
     } 
    } 

    public final class GetYouTubeUserCommentsTask extends 
      AsyncTask<Void, Void, Void> { 

     public static final String LIBRARY = "CommentsLibrary"; 
     private final Handler replyTo; 
     private final String username; 
     String video_id = getIntent().getStringExtra("id"); 

     public GetYouTubeUserCommentsTask(Handler replyTo, String username) { 
      this.replyTo = replyTo; 
      this.username = username; 
     } 


     @Override 
     protected Void doInBackground(Void... arg0) { 
      try { 

       HttpClient client = new DefaultHttpClient(); 

       HttpUriRequest request = new HttpGet(
         "http://gdata.youtube.com/feeds/api/videos/" 
           + video_id 
           + "/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true"); 

       HttpResponse response = client.execute(request); 

       String jsonString = StreamUtils.convertToString(response 
         .getEntity().getContent()); 

       JSONObject json = new JSONObject(jsonString); 
       JSONArray jsonArray = json.getJSONObject("data").getJSONArray(
         "items"); 

       List<Comments> comments = new ArrayList<Comments>(); 

       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 

        String name = jsonObject.getString("name"); 
        String content = jsonObject.getString("content"); 
        String published = jsonObject.getString("published"); 

        comments.add(new Comments(name, content, published)); 

        TextView nameTv = (TextView) findViewById(R.id.name); 
        nameTv.setText(jsonObject.getString("name")); 

        TextView contentTv = (TextView) findViewById(R.id.content); 
        contentTv.setText(jsonObject.getString("content")); 

        TextView publishedTv = (TextView) findViewById(R.id.published); 
        publishedTv.setText(jsonObject.getString("published")); 

       } 

       CommentsLibrary lib = new CommentsLibrary(username, comments); 

       Bundle data = new Bundle(); 
       data.putSerializable(LIBRARY, lib); 

       Message msg = Message.obtain(); 
       msg.setData(data); 
       replyTo.sendMessage(msg); 

      } catch (ClientProtocolException e) { 
       Log.e("Feck", e); 
      } catch (IOException e) { 
       Log.e("Feck", e); 
      } catch (JSONException e) { 
       Log.e("Feck", e); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

     } 
    } 
} 

回答

1

您不能訪問doInBackground()用戶界面 - 請參閱documentation。您需要將結果發送到onPostExecute()並在那裏處理您的UI更新。

+0

我怎麼能忘記... – user3009687

+0

發生在我們所有人在某個時間點。 – 323go