Java 如何在Android应用程序中使用GSON从Youtube API读取JSON-c数据
首先,我是JSON和GSON的初学者,请耐心听我说 我想读取从该链接检索到的数据:Java 如何在Android应用程序中使用GSON从Youtube API读取JSON-c数据,java,android,json,gson,json-c,Java,Android,Json,Gson,Json C,首先,我是JSON和GSON的初学者,请耐心听我说 我想读取从该链接检索到的数据: https://gdata.youtube.com/feeds/api/videos?author=radityadika&v=2&alt=jsonc 因此,我尝试创建一些表示上述链接结果的类: Video.java public class Video implements Serializable { // The title of the video @Serialized
https://gdata.youtube.com/feeds/api/videos?author=radityadika&v=2&alt=jsonc
因此,我尝试创建一些表示上述链接结果的类:
Video.java
public class Video implements Serializable {
// The title of the video
@SerializedName("title")
private String title;
// A link to the video on youtube
@SerializedName("url")
private String url;
// A link to a still image of the youtube video
@SerializedName("thumbUrl")
private String thumbUrl;
@SerializedName("id")
private String id;
public Video(String id, String title, String url, String thumbUrl) {
super();
this.id = id;
this.title = title;
this.url = url;
this.thumbUrl = thumbUrl;
}
/**
* @return the title of the video
*/
public String getTitle(){
return title;
}
/**
* @return the url to this video on youtube
*/
public String getUrl() {
return url;
}
/**
* @return the thumbUrl of a still image representation of this video
*/
public String getThumbUrl() {
return thumbUrl;
}
public String getId() {
return id;
}
}
public class Library implements Serializable{
// The username of the owner of the library
@SerializedName("user")
private String user;
// A list of videos that the user owns
private List<Video> videos;
public Library(String user, List<Video> videos) {
this.user = user;
this.videos = videos;
}
/**
* @return the user name
*/
public String getUser() {
return user;
}
/**
* @return the videos
*/
public List<Video> getVideos() {
return videos;
}
}
Library.java
public class Video implements Serializable {
// The title of the video
@SerializedName("title")
private String title;
// A link to the video on youtube
@SerializedName("url")
private String url;
// A link to a still image of the youtube video
@SerializedName("thumbUrl")
private String thumbUrl;
@SerializedName("id")
private String id;
public Video(String id, String title, String url, String thumbUrl) {
super();
this.id = id;
this.title = title;
this.url = url;
this.thumbUrl = thumbUrl;
}
/**
* @return the title of the video
*/
public String getTitle(){
return title;
}
/**
* @return the url to this video on youtube
*/
public String getUrl() {
return url;
}
/**
* @return the thumbUrl of a still image representation of this video
*/
public String getThumbUrl() {
return thumbUrl;
}
public String getId() {
return id;
}
}
public class Library implements Serializable{
// The username of the owner of the library
@SerializedName("user")
private String user;
// A list of videos that the user owns
private List<Video> videos;
public Library(String user, List<Video> videos) {
this.user = user;
this.videos = videos;
}
/**
* @return the user name
*/
public String getUser() {
return user;
}
/**
* @return the videos
*/
public List<Video> getVideos() {
return videos;
}
}
但是我无法检索数据。this.library
变量,它是library类的一个实例
始终为空
非常感谢您的帮助,如果您需要更多代码,请询问我
非常感谢好的,我可以理解您对JSON和Gson没有任何概念。。。但你有没有至少快速地看一眼或 读了一会儿之后,我建议您使用这个方便的在线工具,以用户友好的方式查看正在检索的JSON数据。您只需在文本选项卡中复制整个JSON,然后单击查看器选项卡 如果这样做,您将看到JSON的结构,如下所示:
{
...
"data": {
...
"items": [
{
"id": "someid",
"title": "sometitle",
...
},
...
]
}
}
现在,您要做的是创建一个表示JSON数据结构的Java类结构,而您并没有这样做!为什么要将属性用户
和视频
添加到库
类中?你认为Gson能神奇地理解你想要检索用户和他的视频吗?它不是那样工作的
为了创建合适的类结构,首先执行以下操作(在伪代码中):
类响应
数据;
类数据
清单项目;
类项目
字符串id;
字符串标题;
因为正如您现在可能已经意识到的,这个类结构确实代表了您的JSON数据!然后根据需要检索的数据添加更多的类和属性(只添加需要检索的类和属性,其余的将自动跳过)。是的,我对JSON所做的唯一一件事就是在PHP中使用JSON编码:D非常感谢您的帮助,我非常感谢。@BlazeTama,没问题,我很高兴这有帮助!你会发现只要稍加练习就很容易了谢谢,现在我终于可以检索到这些数据了。然而,当我想使用这些数据时,我遇到了一个问题。请看一看:如果你有空余时间。谢谢:D