Java JSON数据错误地填充文本视图
我尝试使用JSON填充3个文本视图-我能够将数据输入到文本视图中,但由于某些原因,我得到了不正确的数据(我尝试使用下面JSON响应中的名称、内容和发布值)。我还注意到,当我应该有一个唯一注释列表时,相同的数据会反复出现。我不确定我到底做错了什么,但任何建议都很感激 爪哇:Java JSON数据错误地填充文本视图,java,android,json,youtube,Java,Android,Json,Youtube,我尝试使用JSON填充3个文本视图-我能够将数据输入到文本视图中,但由于某些原因,我得到了不正确的数据(我尝试使用下面JSON响应中的名称、内容和发布值)。我还注意到,当我应该有一个唯一注释列表时,相同的数据会反复出现。我不确定我到底做错了什么,但任何建议都很感激 爪哇: 更改此行,您将无法更新正确的信息列表 list.add(new CommentsLibrary(name, content, published)); 更新 您正在实例化错误类型的对象。您的ArrayList需要Commen
更改此行,您将无法更新正确的信息列表
list.add(new CommentsLibrary(name, content, published));
更新
您正在实例化错误类型的对象。您的ArrayList
需要Comments
,因此在下面进行此更改以实例化Comments
对象
comments.add(new CommentsLibrary(name, content, published));
到
或者,如果CommentsLibrary
实际上是您想要的,则更改您的ArrayList和适配器以接受此类型
让我知道这是否是问题所在。在我看来,列表
不是您要添加项目的内容。您想将它们添加到评论列表中
初始化适配器时,也可以使用comments
Arraylist
CustomAdapter cus = new CustomAdapter(Player.this, comments);
更新2
我相信你从来没有打过setName(name)代码>设置内容(content)代码>setPublished(已发布)代码>
确保在CommentsLibrary.java
更新3
除了我们昨天修复的布局和类存在一些问题之外,您还一直在分析JSON
数据错误。我为您修复了解析逻辑,您可能可以将此代码复制并粘贴到doInBackground()
方法中
应该是这样。用这个方法代替你的方法。一切都会好起来的!
祝你好运
代码
@覆盖
受保护的Void doInBackground(Void…arg0){
DefaultHttpClient=null;
HttpGet getMethod=null;
HttpResponse响应=null;
HttpEntity=null;
urirequesturi=null;
试一试{
client=新的DefaultHttpClient();
requestUri=新的URI(“http://gdata.youtube.com/feeds/api/videos/"
+视频识别码
+“/comments?v=2&alt=json&start index=1&max results=50&prettyprint=true”);
getMethod=newhttpget(requestUri);
response=client.execute(getMethod);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC\u OK){
entity=response.getEntity();
字符串jsonString=EntityUtils.toString(实体);
JSONObject json=新的JSONObject(jsonString);
//从JSON响应获取提要
JSONObject提要=json.getJSONObject(“提要”);
JSONArray entryArray=feeds.getJSONArray(“条目”);
对于(int i=0;i
您能分享完整的JSON响应吗?我绝对可以帮你解决这个问题,但是你必须去为我格式化你的完整JSON响应。您提供的是无效的谢谢!令人惊叹的。谢谢你也可以发布你的评论库
类我需要看看你如何将值分配给你的文本视图
sIs还有什么你可能需要的吗?无关评论:我假设streamusis是你的一个类。android中集成了一个名为EntityUtils的类,它包含一个toString(Entity),它的功能与您的convertToString(您也可以给它一个字符集)几乎相同。谢谢!我几乎有了它。。。不过我有一个小错误:请阅读上面的内容。你不能创建一个ArrayList
并尝试用CommentsLibrary
对象填充它,类型转换可能会起作用,但我想你只是把CommentsLibrary
与Comments
混淆了-好吧-我按照建议实现了它,但我的textView中仍然没有任何内容:(好的,我想是最后一个问题了。在任务执行之前,您正在listview
上调用setAdapter
。因此,当您收到处理程序的响应时,请调用lv.setAdapter(cus)
认为应该解决它!要使此功能正常工作,请将您的列表视图
作为类的成员,并在OnCreate中初始化它。谢谢!您认为我应该在task.execute()之后添加它吗;或
comments.add(new CommentsLibrary(name, content, published));
comments.add(new Comments(name, content, published));
CustomAdapter cus = new CustomAdapter(Player.this, comments);
@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;
}