gpt4 book ai didi

java - JSON 数据错误地填充 textViews

转载 作者:行者123 更新时间:2023-12-03 06:31:04 25 4
gpt4 key购买 nike

我有 3 个 textViews 我正在尝试使用 JSON 填充 - 我能够将数据放入 textViews 但由于某种原因我得到不正确的数据(我正在尝试使用名称、内容的值并在 JSON 响应中发布下面。)我还注意到,当我应该有一个独特的评论列表时,相同的数据会一遍又一遍地重复。我不确定我到底做错了什么,但任何建议都值得赞赏。

java :

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

ListView lv = (ListView) findViewById(R.id.list);
CustomAdapter cus = new CustomAdapter(Player.this, list);
lv.setAdapter(cus);

Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(
handler, 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("feed").getJSONArray(
"entry");

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

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name", "defaultValue");
String content = jsonObject.optString("content",
"defaultValue");
String published = jsonObject.optString("published",
"defaultValue");
list.add(new CommentsLibrary(name, content, published));
}
} 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) {

}

}

public class CustomAdapter extends BaseAdapter {
LayoutInflater mInflater;
ArrayList<CommentsLibrary> list;

public CustomAdapter(Context context, ArrayList<CommentsLibrary> list) {
mInflater = LayoutInflater.from(context);
this.list = list;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView
.findViewById(R.id.textView2);
holder.tv3 = (TextView) convertView
.findViewById(R.id.textView3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(list.get(position).getName());
holder.tv2.setText(list.get(position).getContent());
holder.tv3.setText(list.get(position).getPublished());
return convertView;
}

class ViewHolder {
TextView tv1, tv2, tv3;

}
}
}

JSON响应:
{"encoding":"UTF-8","feed":{"logo":{"$t":"http:\/\/www.gstatic.com\/youtube\/img\/logo.png"},"link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments?v=2","rel":"http:\/\/schemas.google.com\/g\/2005#feed"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/batch?v=2","rel":"http:\/\/schemas.google.com\/g\/2005#batch"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments?alt=json&max-results=50&v=2","rel":"self"},{"type":"application\/atomsvc+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments?alt=atom-service&v=2","rel":"service"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments?alt=json&alt=json&start-token=Gq4ECqsECDISpgQACgAAAEQSAQAgAAgAEAIAgQALIAIAACFAAAAAAACQMCGAYQAkgASQACEAAgEAAAAQAHAAAQQAoQCYAIFGAEAIKgMgBAASASACAAgAAAABBAAKIAAAAJgMIAjARIAAAAUAAAAIAhCDMggSCAQAAAIIAAQEiAAgBAAQQEIIEAAEAACBgShAAiAAAAgAAAIAgQAgAYXEAiMAAgACAAAQAACAQAAQACCAAQQASAQQAAgAAEREAAAAAYABQSAAAABAQAAAhAAKBBAgQAAAACIAAAIECBMCgQBMAAACEEAQAIBAAIQgAIgAJgAAEJgAAgBAABIBCIAAAAgEASABAAIAAAAiAgCAgAAIABgEACEEAAgIAAgAQAAAAAACAAVAWIAQkAiBwAAAJAASACgCAAAECEASBIwwAEAAQhBAADAAAAAAAABAABAxYAACACAhAAEEgAAgQAABAABAARAgISAAAgAAAgYCEAAAYjAAIEABGQEAAABAAAAUAAAAggAAIAELACAJEIAAAABAEEAAg4AEUgABAAggShAADAABASADAAAAACgZRACAAAAEAAAIAAoQCHALIgEAAAAKAAAAAC4AAABAQAAAAAQAQAAAAiAAAAAABBRABAABFEMAQAAEAAIIQABMhAQQAAAYgABCAAIgABAAAMACAAYYIAggABIEBACEoAAIEAEABAEAAAsIQEAAAAAgAAAEEAAAAKFAgAQECAgAASkARhARKJzIiNL4gLsCMJScl6aUtbsCOAFAAQ%3D%3D&max-results=50&v=2","rel":"next"}],"openSearch$totalResults":{"$t":664},"xmlns":"http:\/\/www.w3.org\/2005\/Atom","id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comments"},"author":[{"uri":{"$t":"http:\/\/www.youtube.com\/"},"name":{"$t":"YouTube"}}],"xmlns$openSearch":"http:\/\/a9.com\/-\/spec\/opensearch\/1.1\/","category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"xmlns$gd":"http:\/\/schemas.google.com\/g\/2005","updated":{"$t":"2013-12-16T16:26:06.146Z"},"xmlns$yt":"http:\/\/gdata.youtube.com\/schemas\/2007","gd$etag":"W\/\"C0YCR38_fip7I2A9Wh5UEkw.\"","entry":[{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"He was doin his shit before Logic though. I don't think either of them know \neach other. Wax is a lot older."},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:z13eg1lpnsbjffkvh23bczqysty3dld0f"},"yt$googlePlusUserId":{"$t":"108900457503123436988"},"author":[{"yt$userId":{"$t":"CHSZNY4x6F6pWRJobUoFYQ"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/CHSZNY4x6F6pWRJobUoFYQ"},"name":{"$t":"Aaron Tucci"}}],"title":{"$t":"He was doin his ..."},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-11-20T17:14:52.000Z"},"gd$etag":"W\/\"DUENQ347eCp7I2A9Wh5XGUs.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/z13eg1lpnsbjffkvh23bczqysty3dld0f?v=2","rel":"self"}],"published":{"$t":"2013-11-20T17:14:52.000Z"},"yt$channelId":{"$t":"UCCHSZNY4x6F6pWRJobUoFYQ"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"This will play at my funeral"},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:z12qdzqpvtzzgb3wn04cefsy5yfbtpwgfr40k"},"yt$googlePlusUserId":{"$t":"114722569313545191091"},"author":[{"yt$userId":{"$t":"SSYvgm-__QWJ-QBn7WBCOA"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/EvincentDavis73"},"name":{"$t":"EvincentDavis73"}}],"title":{"$t":"This will play at ..."},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-11-25T22:03:55.000Z"},"gd$etag":"W\/\"DUcHRH47eCp7I2A9Wh5WFEw.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/z12qdzqpvtzzgb3wn04cefsy5yfbtpwgfr40k?v=2","rel":"self"}],"published":{"$t":"2013-11-25T22:03:55.000Z"},"yt$channelId":{"$t":"UCSSYvgm-__QWJ-QBn7WBCOA"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"Damn Wax, will always be chill"},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:ENZPbF_dkcQPL32E3PWUkDO2TriUzzdwwutHskE4KyI"},"author":[{"yt$userId":{"$t":"HvHwTN1oQo8ReLJizlGeTA"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/HvHwTN1oQo8ReLJizlGeTA"},"name":{"$t":"Mark Henry Salvador"}}],"title":{"$t":"Damn Wax, will ..."},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-10-22T17:57:38.000Z"},"gd$etag":"W\/\"DkEBSX47eCp7I2A9Wh5RFEs.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/ENZPbF_dkcQPL32E3PWUkDO2TriUzzdwwutHskE4KyI?v=2","rel":"self"}],"published":{"$t":"2013-10-22T17:57:38.000Z"},"yt$channelId":{"$t":"UCHvHwTN1oQo8ReLJizlGeTA"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"Chill song"},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:ENZPbF_dkcS4GpS8ZITWuVUsUYNyrGQGWajSGKIXO38"},"author":[{"yt$userId":{"$t":"hXnILfiYMX0Li4Ok99RDVQ"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/hXnILfiYMX0Li4Ok99RDVQ"},"name":{"$t":"thedudesons55xD"}}],"title":{"$t":"Chill song"},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-10-18T17:38:09.000Z"},"gd$etag":"W\/\"DU8MSH47eCp7I2A9Wh5REUw.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/ENZPbF_dkcS4GpS8ZITWuVUsUYNyrGQGWajSGKIXO38?v=2","rel":"self"}],"published":{"$t":"2013-10-18T17:38:09.000Z"},"yt$channelId":{"$t":"UChXnILfiYMX0Li4Ok99RDVQ"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"Jammed to this song at the Lancaster show, just fuckin great."},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:ENZPbF_dkcRbJyAUdl6-zrqjvoZsxzicrhGx2ZovIaI"},"author":[{"yt$userId":{"$t":"-EhhQyun4YBw_LvMZXwXRg"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/-EhhQyun4YBw_LvMZXwXRg"},"name":{"$t":"RadiationDubstep"}}],"title":{"$t":"Jammed to this song ..."},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-10-17T19:32:12.000Z"},"gd$etag":"W\/\"AkQHQ347eCp7I2A9Wh5REE4.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/ENZPbF_dkcRbJyAUdl6-zrqjvoZsxzicrhGx2ZovIaI?v=2","rel":"self"}],"published":{"$t":"2013-10-17T19:32:12.000Z"},"yt$channelId":{"$t":"UC-EhhQyun4YBw_LvMZXwXRg"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"he sounds like logic"},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:ENZPbF_dkcSsgrj7dLTiPt_IbMf0WCv3vPaBnNorDEE"},"author":[{"yt$userId":{"$t":"IVTqw7FaRUFdTgK6-XCJsg"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/IVTqw7FaRUFdTgK6-XCJsg"},"name":{"$t":"Johnny Trikhin"}}],"title":{"$t":"he sounds like logic"},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-10-17T03:06:36.000Z"},"gd$etag":"W\/\"A0YNR347eCp7I2A9Wh5SGUo.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns?v=2","rel":"related"},{"type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=AaWp8YZi1Ns","rel":"alternate"},{"type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/AaWp8YZi1Ns\/comments\/ENZPbF_dkcSsgrj7dLTiPt_IbMf0WCv3vPaBnNorDEE?v=2","rel":"self"}],"published":{"$t":"2013-10-17T03:06:36.000Z"},"yt$channelId":{"$t":"UCIVTqw7FaRUFdTgK6-XCJsg"},"yt$replyCount":{"$t":0}},{"yt$videoid":{"$t":"AaWp8YZi1Ns"},"content":{"$t":"Hats off to Wax AND EOM, people don't give E enough credit"},"id":{"$t":"tag:youtube.com,2008:video:AaWp8YZi1Ns:comment:ENZPbF_dkcT6kdus1Qc0wE8XcjPxgFjuZ26nZfV8EQM"},"author":[{"yt$userId":{"$t":"tmYlMT3VSyjLOt6KXeTJjg"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/tmYlMT3VSyjLOt6KXeTJjg"},"name":{"$t":"PZMCWEequalsMC3"}}],"title":{"$t":"Hats off to Wax AND ..."},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#comment"}],"updated":{"$t":"2013-10-08T04:31:11.000Z"},"gd$etag":"W\/\"DEEDQH47eCp7I2A9Wh5SEk0.\"","link":[{"type":"application\/atom+xml","href":"http:\/\/gdata.you...

enter image description here

最佳答案

更改此行,您没有更新正确的信息列表

list.add(new CommentsLibrary(name, content, published));

更新

您正在实例化错误类型的对象。您的 ArrayList预计 Comments因此,请在下面进行此更改以实例化 Comments目的。
comments.add(new CommentsLibrary(name, content, published));


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

或者如果 CommentsLibrary实际上是您想要的然后更改您的 ArrayList 和适配器以接受此类型。

让我知道这是否是问题所在。在我看来它像 list不是您想要添加项目的对象。并且您想将它们添加到评论 ArrayList

并且在初始化适配器时使用 comments数组列表也是
CustomAdapter cus = new CustomAdapter(Player.this, comments);

更新 2

我相信你从来没有调用 setName(name); setContent(content); setPublished(published);确保您实际上是在 CommentsLibrary.java 中的构造函数中调用这些

更新 3

除了实际上我们昨天修复的布局和类有一些问题之外,您还一直是 parsing JSON数据错误。我为您修复了解析逻辑,您可能可以将此代码复制并粘贴到您的 doInBackground() 中。方法。

应该是这样的。只需用这个替换你的方法。一切都应该很好!
祝你好运。

代码
 @Override
protected Void doInBackground(Void... arg0) {

DefaultHttpClient client = null;

HttpGet getMethod = null;

HttpResponse response = null;

HttpEntity entity = null;

URI requestUri = null;


try {

client = new DefaultHttpClient();

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

getMethod = new HttpGet(requestUri);

response = client.execute(getMethod);

if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){

entity = response.getEntity();

String jsonString = EntityUtils.toString(entity);

JSONObject json = new JSONObject(jsonString);

// Get the Feed From the JSON Response
JSONObject feeds = json.getJSONObject("feed");
JSONArray entryArray = feeds.getJSONArray("entry");

for(int i = 0; i < entryArray.length(); i++){

// Get the entry at index 0
JSONObject entry = entryArray.getJSONObject(i);

// Get the Published data
JSONObject publishedObject = entry.getJSONObject("published");
published = publishedObject.getString("$t");


// Get the Content of the Comment
JSONObject contentObject = entry.getJSONObject("content");
content = contentObject.getString($t);

// Get the Author of the content
JSONArray commentNames = entry.getJSONArray("author");
for(int j = 0; j < commentNames.length(); j++){

// Get the Author Object at the first index
JSONObject singleAuthor = commentNames.getJSONObject(j);
JSONObject authorName = singleAuthor.getJSONObject("name");

name = authorName.getString("$t");
}

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

}

}
}catch (ClientProtocolException e) {
Log.d("ClientProtocolException", ""+ e);

}catch(URISyntaxException e){

Log.d("URISyntaxException",""+ e);
} catch (IOException e) {
Log.d("IOException",""+ e);
} catch (JSONException e) {
Log.d("JSONException",""+ e);
}
return null;
}

关于java - JSON 数据错误地填充 textViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20615928/

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