gpt4 book ai didi

java - JSON 查询不更新 TextView

转载 作者:行者123 更新时间:2023-11-30 11:24:53 26 4
gpt4 key购买 nike

我有 3 个 TextView ,我正在尝试使用通过 JSON 提取的数据进行更新 - 到目前为止我构建的内容没有出现任何错误 - 但 TextView (nameTv、contentTv 和 publishedTv)似乎没有使用我的 JSON 查询中的数据进行更新。

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) {

}
}
}

最佳答案

您无法在 doInBackground() 中访问 UI -- 请参阅 documentation .您需要将结果传送到 onPostExecute() 并在那里处理您的 UI 更新。

关于java - JSON 查询不更新 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com