JSON字符串被错误地映射到textviews [英] JSON String is incorrectly mapped to textviews
问题描述
我有3个textviews这我试图用来自一个JSON HTTP响应但是他们目前正在填充了响应不正确的数据。
有关某种原因,它是不解析字段和正确地分配给它们 - 看来它简单地填充每个的TextView与来自JSON响应的数据的开头。
截图:
JSON HTTP响应:
{
版本:1.0,
编码:UTF-8,
饲料: {
的xmlns:http://www.w3.org/2005/Atom,
的xmlns $ OpenSearch的:http://a9.com/-/spec/opensearch/1.1/
的xmlns $ GD:http://schemas.google.com/g/2005
的xmlns $ YT:http://gdata.youtube.com/schemas/2007
GD $ ETAG:W / \Ak8ASXk-fCp7I2A9Wh5VGEg \,
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论
},
更新:{
$ T:2013-12-12T14:34:08.754Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标识:{
$ T:http://www.gstatic.com/youtube/img/logo.png
},
链接: [
{
相对:http://schemas.google.com/g/2005#feed
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?v=2
},
{
相对:http://schemas.google.com/g/2005#batch
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/batch?v=2
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&max-results=50&v=2
},
{
相对:服务,
类型:应用程序/ atomsvc + XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=atom-service&v=2
},
{
相对:下一步,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&alt=json&start-token=Gq4ECqsECDISpgQACgAAAEQAJYAEABUBAAACAJUIACgAIAABABAAABAgAAAQABAALSAAAgQQBQBAAQAgRgAQAAABAAAJBAAAAkACBAAIAACYCgAIABBAIAAACAAAQAABAEAAAAAAEAADACCAABgAggAACgIQgAAAAAEICAlRk0YAgAAAAgAASAAIEgUQAAhAgACAAIgABAIgAAAAAAAiAAAAABSMQCAgAAAAAAIAASAAgAhQggABASAAAAAiADQQiAAgSAKQGIAABAAANAAAgAGSAAgEEAhABkAAAAEhCBAAgEAoAIAIAggCgAQACCICgQAgAAAoAAAgoCQAAAAEgEEAAAgAAkEEGAECACASAAiQADAAAIKAGKAABAEAKAAAQCEBCAIAABAAAAAACGAACAIIHABACAABRAEAAQAAQgAAAwGKoABIAAAkABAEAaAJAAgIKAAACgARAARAgoJEQCBIEAABBAEAAAFEAAAyQAAAACCBgAUCAghACBIFAIAwBAAgACBFACBAgAQAFQACAEAgACBEAgBAIFgJAwBIAADECCgAQAAAUIAAgAAQAAoAIDUAgIAAABAAAAmAAgAACiACAgiBEAAAABCBAwBACQggAB4CAoAABIBAKBAACBAISBgAqAEQEIUQRAIAAADCBCAAEQCAACBAAiAAAABDCIAAAAAAQAAAAEQCBwABAAIIAAAIAgECAAAkAgQABAAQAAAAgEERCAAIAQAgAAABAAAAKOiX8qy6oLsCMLmMmejzqrsCOAFAAQ%3D%3D&max-results=50&v=2"
}
]
作者: [
{
名称: {
$ T:YouTube的视频
},
URI:{
$ T:http://www.youtube.com/
}
}
]
发电机: {
$ T:YouTube的数据API,
版本:2.1,
URI:http://gdata.youtube.com
},
OpenSearch的$ totalResults:{
$ T:75
},
OpenSearch的$ itemsPerPage:{
$ T:50
},
条目: [
{
GD $ ETAG:W / \A08GR347eCp7I2A9Wh5VFEQ \,。
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论:z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs
},
发表:{
$ T:2013-12-08T10:50:26.000Z
},
更新:{
$ T:2013-12-08T10:50:26.000Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标题: {
$ T:SUPER [马里奥]巢......
},
内容: {
$ T:SUPER [马里奥]晁!!!!!!!!!!!!!!!!!! \ ufeff
},
链接: [
{
相对:相关,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2
},
{
相对:候补,
类型:text / html的,
HREF:http://www.youtube.com/watch?v=PYvyGZwHlKg
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs?v=2"
}
]
作者: [
{
名称: {
$ T:最大Elymanov
},
URI:{
$ T:http://gdata.youtube.com/feeds/api/users/answer37
},
YT $用户id:{
$ T:J4HQsbLcybybImJBXr4hmA
}
}
]
YT $的channelID:{
$ T:UCJ4HQsbLcybybImJBXr4hmA
},
YT $ googlePlusUserId:{
$ T:104133629856589391856
},
YT $ replyCount:{
$ T:0
},
YT $标识(VideoID):{
$ T:PYvyGZwHlKg
}
},
{
GD $ ETAG:W / \C04MRH47eCp7I2A9Wh5SFEk \,
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论:U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-TB-aMCsRM
},
发表:{
$ T:2013-10-10T21:53:05.000Z
},
更新:{
$ T:2013-10-10T21:53:05.000Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标题: {
$ T:OOOOOO最好的...
},
内容: {
$ T:OOOOOO最好saludoss马努chaoo修订于乌夫里克(卡迪斯)
},
链接: [
{
相对:相关,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2
},
{
相对:候补,
类型:text / html的,
HREF:http://www.youtube.com/watch?v=PYvyGZwHlKg
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-Tb-aMCsRM?v=2"
}
]
作者: [
{
名称: {
$ T:胡安·安东尼奥加戈
},
URI:{
$ T:http://gdata.youtube.com/feeds/api/users/u22Kbw-Z9GDEYLcuHBMX_w
},
YT $用户id:{
$ T:u22Kbw-Z9GDEYLcuHBMX_w
}
}
]
YT $的channelID:{
$ T:UCu22Kbw-Z9GDEYLcuHBMX_w
},
YT $ replyCount:{
$ T:0
},
YT $标识(VideoID):{
$ T:PYvyGZwHlKg
}
},
{
GD $ ETAG:W / \CUEMRX47eCp7I2A9WhFaEEQ \,。
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论:U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8
},
发表:{
$ T:2013-09-13T17:34:44.000Z
},
更新:{
$ T:2013-09-13T17:34:44.000Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标题: {
$ T:peligrosamente ......
},
内容: {
$ T:peligrosamente EXCELENTE!没有TENGO palabras对definir LO布埃诺q \ nsiento q埃斯特ES协奏曲...亲切!
},
链接: [
{
相对:相关,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2
},
{
相对:候补,
类型:text / html的,
HREF:http://www.youtube.com/watch?v=PYvyGZwHlKg
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8?v=2"
}
]
作者: [
{
名称: {
$ T:PALOMA ROSAS
},
URI:{
$ T:http://gdata.youtube.com/feeds/api/users/3myOrO39fEOgdJt9UAoP3g
},
YT $用户id:{
$ T:3myOrO39fEOgdJt9UAoP3g
}
}
]
YT $的channelID:{
$ T:UC3myOrO39fEOgdJt9UAoP3g
},
YT $ replyCount:{
$ T:0
},
YT $标识(VideoID):{
$ T:PYvyGZwHlKg
}
},
{
GD $ ETAG:W / \CE8EQX47eCp7I2A9WhFbEEo \,
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论:U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw
},
发表:{
$ T:2013-09-01T22:00:00.000Z
},
更新:{
$ T:2013-09-01T22:00:00.000Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标题: {
$ T:coincido 2000%
},
内容: {
$ T:coincido 2000%
},
链接: [
{
相对:相关,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2
},
{
相对:候补,
类型:text / html的,
HREF:http://www.youtube.com/watch?v=PYvyGZwHlKg
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw?v=2"
}
]
作者: [
{
名称: {
$ T:胡安·阿古斯
},
URI:{
$ T:http://gdata.youtube.com/feeds/api/users/0Fm76n0qFloO9YGwf9sYTg
},
YT $用户id:{
$ T:0Fm76n0qFloO9YGwf9sYTg
}
}
]
YT $的channelID:{
$ T:UC0Fm76n0qFloO9YGwf9sYTg
},
YT $ replyCount:{
$ T:0
},
YT $标识(VideoID):{
$ T:PYvyGZwHlKg
}
},
{
GD $ ETAG:W / \CkcHSH47eCp7I2A9WhVXFEg \,
ID:{
$ T:标签:youtube.com,2008:视频:PYvyGZwHlKg:评论:U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ - 茵莱
},
发表:{
$ T:2012-04-15T00:33:59.000Z
},
更新:{
$ T:2012-04-15T00:33:59.000Z
},
类别: [
{
计划:http://schemas.google.com/g/2005#kind
术语:http://gdata.youtube.com/schemas/2007#comment
}
]
标题: {
$ T:玛奴乔小仔......
},
内容: {
$ T:玛奴乔小仔TROP盂兰盆花莲杜博纳尔视频采访演唱会yeaaaahh !!!!
},
链接: [
{
相对:相关,
类型:应用程序/原子+ XML,
HREF:http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2
},
{
相对:候补,
类型:text / html的,
HREF:http://www.youtube.com/watch?v=PYvyGZwHlKg
},
{
相对:自我,
类型:应用程序/原子+ XML,
HREF: "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ-INlE?v=2"
...
79900
0
0
JAVA:
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.player);
字符串标题= getIntent()getStringExtra(标题)。
字符串上传= getIntent()getStringExtra(上传)。
串观看次数= getIntent()getStringExtra(观看次数)。
TextView的titleTv =(TextView中)findViewById(R.id.titleTv);
TextView的uploaderTv =(TextView中)findViewById(R.id.uploaderTv);
TextView的viewCountTv =(TextView中)findViewById(R.id.viewCountTv);
titleTv.setText(职称);
uploaderTv.setText(本经+上传+|);
viewCountTv.setText(观看次数+意见);
YouTubePlayerView youTubePlayerView =(YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY,这一点);
处理程序处理程序=新的处理程序(新Handler.Callback(){
@覆盖
公共布尔的handleMessage(信息MSG){
返回false;
}
});
GetYouTubeUserCommentsTask任务=新GetYouTubeUserCommentsTask(处理器,观看次数);
task.execute();
}
@覆盖
公共无效onInitializationFailure(供应商提供,
YouTubeInitializationResult结果){
Toast.makeText(getApplicationContext(),onInitializationFailure(),
Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onInitializationSuccess(供应商提供,
YouTubePlayer球员,布尔wasRestored){
如果(!wasRestored){
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
player.loadVideo(VIDEO_ID);
}
}
公共final类GetYouTubeUserCommentsTask扩展
AsyncTask的<虚空,虚空,虚空> {
公共静态最终字符串库=CommentsLibrary;
私人最终处理程序的replyTo;
私人最终字符串用户名;
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
公共GetYouTubeUserCommentsTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo;
this.username =用户名;
}
@覆盖
保护无效doInBackground(虚空......为arg0){
尝试 {
HttpClient的客户端=新DefaultHttpClient();
HttpUriRequest请求=新HTTPGET(
http://gdata.youtube.com/feeds/api/videos/
+ VIDEO_ID
+/评论ν= 2及?中高音= JSON&安培;启动索引= 1&安培;最大-结果= 50安培; prettyprint =真);
HTT presponse响应= client.execute(要求);
字符串jsonString = StreamUtils.convertToString(响应
。.getEntity()的getContent());
JSONObject的JSON =新的JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject(饲料)。getJSONArray(
条目);
名单<意见>评论=新的ArrayList<意见>();
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
字符串名称= jsonObject.optString(名,设置defaultValue);
字符串的内容= jsonObject.optString(内容,设置defaultValue);
字符串公布= jsonObject.optString(发布,设置defaultValue);
comments.add(新评论(名称,内容,出版));
}
CommentsLibrary LIB =新CommentsLibrary(jsonString,jsonString,jsonString);
捆绑数据=新包();
data.putSerializable(图书馆,LIB);
消息味精= Message.obtain();
msg.setData(数据);
replyTo.sendMessage(MSG);
}赶上(ClientProtocolException E){
Log.e(Feck,E);
}赶上(IOException异常E){
Log.e(Feck,E);
}赶上(JSONException E){
Log.e(Feck,E);
}
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
TextView的nameTv =(TextView中)findViewById(R.id.name);
nameTv.setText(com.idg.omv.domain.CommentsLibrary.getName());
TextView的contentTv =(TextView中)findViewById(R.id.content);
contentTv.setText(com.idg.omv.domain.CommentsLibrary.getContent());
TextView的publishedTv =(TextView中)findViewById(R.id.published);
publishedTv.setText(com.idg.omv.domain.CommentsLibrary.getPublished());
}
}
}
CommentsLibrary
公共类CommentsLibrary实现Serializable {
//注释的所有者的用户名
私有静态字符串名称;
//注释
私有静态字符串的内容;
//日期注释出版
私有静态字符串出版;
公共CommentsLibrary(字符串名称,字符串内容,字符串出版){
this.name =名称;
this.content =内容;
this.published =出版;
}
/ **
返回:用户名
* /
公共静态字符串的getName(){
返回名称;
}
/ **
返回:视频
* /
公共静态字符串的getContent(){
返回的内容;
}
/ **
返回:视频
* /
公共静态字符串出版文章(){
返回出版;
}
}
您
CommentsLibrary LIB =新CommentsLibrary(jsonString,jsonString,jsonString);
所有PARAMS传递给CommentsLibrary的构造函数是 jsonString
。所有三个具有相同jsonstring。
公共CommentsLibrary(字符串名称,字符串内容,字符串出版){
this.name =名称;
this.content =内容;
this.published =出版;
}
所以它,当你在相应的领域获得方法显示相同的内容
编辑:
声明列表作为一个实例变量
的ArrayList< CommentsLibrary>名单=新的ArrayList< CommentsLibrary>();
然后
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
字符串名称= jsonObject.optString(名,设置defaultValue);
字符串的内容= jsonObject.optString(内容,设置defaultValue);
字符串公布= jsonObject.optString(发布,设置defaultValue);
list.add(新CommentsLibrary(名称,内容,出版));
}
然后在 onPostExecute
使用一个ListView难熬自定义适配器和填充ListView与 ArrayList的名单
。
使用文本视图,你只能看到1组值。
I have 3 textviews which I'm attempting to populate with data from a JSON Http response however they are currently being populated with the incorrect data from the response.
For some reason it isn't parsing the fields and assigning them correctly - it appears it simply fills each textview with the beginning of the data from the JSON response.
SCREENSHOT:
JSON HTTP RESPONSE:
{
"version": "1.0",
"encoding": "UTF-8",
"feed": {
"xmlns": "http://www.w3.org/2005/Atom",
"xmlns$openSearch": "http://a9.com/-/spec/opensearch/1.1/",
"xmlns$gd": "http://schemas.google.com/g/2005",
"xmlns$yt": "http://gdata.youtube.com/schemas/2007",
"gd$etag": "W/\"Ak8ASXk-fCp7I2A9Wh5VGEg.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comments"
},
"updated": {
"$t": "2013-12-12T14:34:08.754Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"logo": {
"$t": "http://www.gstatic.com/youtube/img/logo.png"
},
"link": [
{
"rel": "http://schemas.google.com/g/2005#feed",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?v=2"
},
{
"rel": "http://schemas.google.com/g/2005#batch",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/batch?v=2"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&max-results=50&v=2"
},
{
"rel": "service",
"type": "application/atomsvc+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=atom-service&v=2"
},
{
"rel": "next",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&alt=json&start-token=Gq4ECqsECDISpgQACgAAAEQAJYAEABUBAAACAJUIACgAIAABABAAABAgAAAQABAALSAAAgQQBQBAAQAgRgAQAAABAAAJBAAAAkACBAAIAACYCgAIABBAIAAACAAAQAABAEAAAAAAEAADACCAABgAggAACgIQgAAAAAEICAlRk0YAgAAAAgAASAAIEgUQAAhAgACAAIgABAIgAAAAAAAiAAAAABSMQCAgAAAAAAIAASAAgAhQggABASAAAAAiADQQiAAgSAKQGIAABAAANAAAgAGSAAgEEAhABkAAAAEhCBAAgEAoAIAIAggCgAQACCICgQAgAAAoAAAgoCQAAAAEgEEAAAgAAkEEGAECACASAAiQADAAAIKAGKAABAEAKAAAQCEBCAIAABAAAAAACGAACAIIHABACAABRAEAAQAAQgAAAwGKoABIAAAkABAEAaAJAAgIKAAACgARAARAgoJEQCBIEAABBAEAAAFEAAAyQAAAACCBgAUCAghACBIFAIAwBAAgACBFACBAgAQAFQACAEAgACBEAgBAIFgJAwBIAADECCgAQAAAUIAAgAAQAAoAIDUAgIAAABAAAAmAAgAACiACAgiBEAAAABCBAwBACQggAB4CAoAABIBAKBAACBAISBgAqAEQEIUQRAIAAADCBCAAEQCAACBAAiAAAABDCIAAAAAAQAAAAEQCBwABAAIIAAAIAgECAAAkAgQABAAQAAAAgEERCAAIAQAgAAABAAAAKOiX8qy6oLsCMLmMmejzqrsCOAFAAQ%3D%3D&max-results=50&v=2"
}
],
"author": [
{
"name": {
"$t": "YouTube"
},
"uri": {
"$t": "http://www.youtube.com/"
}
}
],
"generator": {
"$t": "YouTube data API",
"version": "2.1",
"uri": "http://gdata.youtube.com"
},
"openSearch$totalResults": {
"$t": 75
},
"openSearch$itemsPerPage": {
"$t": 50
},
"entry": [
{
"gd$etag": "W/\"A08GR347eCp7I2A9Wh5VFEQ.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs"
},
"published": {
"$t": "2013-12-08T10:50:26.000Z"
},
"updated": {
"$t": "2013-12-08T10:50:26.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "SUPER [MARIO] CHAO ..."
},
"content": {
"$t": "SUPER [MARIO] CHAO !!!!!!!!!!!!!!!!!!\ufeff"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs?v=2"
}
],
"author": [
{
"name": {
"$t": "Max Elymanov"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/answer37"
},
"yt$userId": {
"$t": "J4HQsbLcybybImJBXr4hmA"
}
}
],
"yt$channelId": {
"$t": "UCJ4HQsbLcybybImJBXr4hmA"
},
"yt$googlePlusUserId": {
"$t": "104133629856589391856"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"C04MRH47eCp7I2A9Wh5SFEk.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-Tb-aMCsRM"
},
"published": {
"$t": "2013-10-10T21:53:05.000Z"
},
"updated": {
"$t": "2013-10-10T21:53:05.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "oooooo the best ..."
},
"content": {
"$t": "oooooo the best saludoss manu chaoo desde ubrique ( cadiz )"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-Tb-aMCsRM?v=2"
}
],
"author": [
{
"name": {
"$t": "Juan Antonio Gago"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/u22Kbw-Z9GDEYLcuHBMX_w"
},
"yt$userId": {
"$t": "u22Kbw-Z9GDEYLcuHBMX_w"
}
}
],
"yt$channelId": {
"$t": "UCu22Kbw-Z9GDEYLcuHBMX_w"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CUEMRX47eCp7I2A9WhFaEEQ.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8"
},
"published": {
"$t": "2013-09-13T17:34:44.000Z"
},
"updated": {
"$t": "2013-09-13T17:34:44.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "peligrosamente ..."
},
"content": {
"$t": "peligrosamente excelente!!! no tengo palabras para definir lo bueno q \nsiento q este concierto es... genial!"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8?v=2"
}
],
"author": [
{
"name": {
"$t": "PALOMA ROSAS"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/3myOrO39fEOgdJt9UAoP3g"
},
"yt$userId": {
"$t": "3myOrO39fEOgdJt9UAoP3g"
}
}
],
"yt$channelId": {
"$t": "UC3myOrO39fEOgdJt9UAoP3g"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CE8EQX47eCp7I2A9WhFbEEo.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw"
},
"published": {
"$t": "2013-09-01T22:00:00.000Z"
},
"updated": {
"$t": "2013-09-01T22:00:00.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "coincido 2000%"
},
"content": {
"$t": "coincido 2000%"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw?v=2"
}
],
"author": [
{
"name": {
"$t": "juan agus"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/0Fm76n0qFloO9YGwf9sYTg"
},
"yt$userId": {
"$t": "0Fm76n0qFloO9YGwf9sYTg"
}
}
],
"yt$channelId": {
"$t": "UC0Fm76n0qFloO9YGwf9sYTg"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CkcHSH47eCp7I2A9WhVXFEg.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ-INlE"
},
"published": {
"$t": "2012-04-15T00:33:59.000Z"
},
"updated": {
"$t": "2012-04-15T00:33:59.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "manu chao c'est ..."
},
"content": {
"$t": "manu chao c'est trop bon c'est du bonheur en vidéo concert yeaaaahh !!!!"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ-INlE?v=2"
...
79900
0
0
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);
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");
comments.add(new Comments(name, content, published));
}
CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
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) {
TextView nameTv = (TextView) findViewById(R.id.name);
nameTv.setText(com.idg.omv.domain.CommentsLibrary.getName());
TextView contentTv = (TextView) findViewById(R.id.content);
contentTv.setText(com.idg.omv.domain.CommentsLibrary.getContent());
TextView publishedTv = (TextView) findViewById(R.id.published);
publishedTv.setText(com.idg.omv.domain.CommentsLibrary.getPublished());
}
}
}
CommentsLibrary
public class CommentsLibrary implements Serializable{
// The username of the owner of the comment
private static String name;
// The comment
private static String content;
// The date the comment was published
private static String published;
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
/**
* @return the user name
*/
public static String getName() {
return name;
}
/**
* @return the videos
*/
public static String getContent() {
return content;
}
/**
* @return the videos
*/
public static String getPublished() {
return published;
}
}
You have
CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
All params passed to the constructor of CommentsLibrary is jsonString
. All the three have the same jsonstring.
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
So its the same content that is displayed when you get method for the corresponding fields
Edit:
Declare list as a instance variable
ArrayList<CommentsLibrary> list = new ArrayList<CommentsLibrary>();
Then
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));
}
Then in onPostExecute
Use a ListView witha custom adapter and populate the listview with the ArrayList list
.
With text view you only see 1 set of values.
这篇关于JSON字符串被错误地映射到textviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!