【问题标题】:How to read JSON-c data from Youtube API using GSON in Android Apps如何在 Android 应用程序中使用 GSON 从 Youtube API 读取 JSON-c 数据
【发布时间】:2013-09-25 14:05:08
【问题描述】:

首先,我是 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
    @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;
    }
}

库.java

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;
    }
}

之后,我尝试使用这些代码检索数据:

@Override
    public void run() {
        try {
            // Get a httpclient to talk to the internet
            HttpClient client = new DefaultHttpClient();
            // Perform a GET request to YouTube for a JSON list of all the videos by a specific user
            HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc");
            //HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?title="+username+"&v=2&alt=jsonc");
            // Get the response that YouTube sends back
            HttpResponse response = client.execute(request);
            // Convert this response into a readable string
            //String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
            final int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) { 
                //Log.w(getClass().getSimpleName(), "Error " + statusCode);
            }
            // Create a JSON object that we can use from the String
            //JSONObject json = new JSONObject(jsonString);

            HttpEntity getResponseEntity = response.getEntity();
            InputStream httpResponseStream = getResponseEntity.getContent();
            Reader inputStreamReader = new InputStreamReader(httpResponseStream);

            Gson gson = new Gson();
            this.library = gson.fromJson(inputStreamReader, Library.class);


        } catch (Exception e) {
            Log.e("Feck", e);
        }
    }

但是我无法检索数据。 this.library 变量,它是Library Class 的一个实例,始终为空。

感谢任何帮助,如果您需要更多代码,请询问我。

非常感谢

【问题讨论】:

    标签: java android json gson json-c


    【解决方案1】:

    好的,我可以理解您对 JSON 和 Gson 没有任何想法...但是您是否至少快速浏览过 JSON specificationsGson documentation

    看了一会儿,我建议你使用这个方便的在线JSON Viewer 以一种用户友好的方式查看你正在检索的 JSON 数据。您只需将整个 JSON 复制到 Text 选项卡中,然后单击 Viewer 选项卡...

    如果您这样做,您将看到 JSON 的结构,如下所示:

    {
      ...
      "data": {
        ...
        "items": [
          {
            "id": "someid",
            "title": "sometitle",
            ...
          },
          ...
        ]
      }
    }
    

    现在,您要做的是创建一个 Java 类结构来表示您的 JSON 的数据结构,而您并没有这样做!为什么将属性uservideos 添加到Library 类中?您是否认为 Gson 可以神奇地理解您要检索用户和他的视频?真的不行啊……

    为了创建合适的类结构,首先做这样的事情(在伪代码中):

    class Response
      Data data;
    
    class Data
      List<Item> items;
    
    class Item
      String id;
      String title;
    

    因为您现在可能已经意识到,这个类结构确实代表了您的 JSON 数据!然后根据您要检索的数据添加更多的类和属性(只添加您需要检索的那些类和属性,其余的将被自动跳过)。

    【讨论】:

    • 是的,我对 JSON 所做的唯一一件事就是在 PHP 中使用 json_encode :D 非常感谢您的帮助,我真的很感激。
    • @BlazeTama,没问题,我很高兴它有帮助!你会发现只要稍微练习一下就很容易了
    • 多亏了你,现在我终于可以检索到这些数据了。但是,当我想使用这些数据时遇到了问题。如果您有空闲时间,请查看:stackoverflow.com/questions/19026759/…。谢谢:D
    猜你喜欢
    • 2012-11-27
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多