2013-12-11 3 views
2

JSON을 통해 가져온 데이터로 업데이트하려고 시도하는 3 개의 textview가 있습니다 - 지금까지 작성한 것과 관련하여 오류가 발생하지 않지만 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()의 UI에 액세스 할 수 없습니다 - documentation를 참조하십시오. 결과를 onPostExecute()에게 전달하고 UI 업데이트를 처리해야합니다.

+0

어떻게 잊을 수 있니 ... – user3009687

+0

어느 시점에서 우리 모두에게 일어났습니다. – 323go

관련 문제