2

特定のユーザーの動画を取得する Android アプリを開発しようとしています。JSON-C 応答形式を使用してサイズを低く抑えたいと考えています。応答が得られたら、次の形式になります。

{
"apiVersion":"2.1",
"data":{
    "updated":"2012-12-02T14:40:07.424Z",
    "totalItems":175,
    "startIndex":1,
    "itemsPerPage":25,
    "items":[
        {
            "id":"rnzJ5x3sWyk",
            "uploaded":"2012-12-01T20:19:07.000Z",
            "updated":"2012-12-01T20:19:07.000Z",
            "uploader":"user",
            "category":"News",
            "title":"L'ALLUVIONE HA DISTRUTTO LE ROULOTTE DELLE VACANZE",
            "description":"Ad Albinia,in un rimessaggio per roulotte, il proprietario, i volontari e i vacanzieri che sono soliti passare l'estate qui, si sono rimboccati le maniche per ripulire dal fango le tante \"case delle vacanze\" distrutte dall'alluvione",
            "thumbnail":{
                "sqDefault":"http://i.ytimg.com/vi/rnzJ5x3sWyk/default.jpg",
                "hqDefault":"http://i.ytimg.com/vi/rnzJ5x3sWyk/hqdefault.jpg"
            },
            "player":{
                "default":"http://www.youtube.com/watch?v=rnzJ5x3sWyk&feature=youtube_gdata_player",
                "mobile":"http://m.youtube.com/details?v=rnzJ5x3sWyk"
            },
            "content":{
                "5":"http://www.youtube.com/v/rnzJ5x3sWyk?version=3&f=user_uploads&app=youtube_gdata",
                "1":"rtsp://v1.cache2.c.youtube.com/CigLENy73wIaHwkpW-wd58l8rhMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp",
                "6":"rtsp://v6.cache2.c.youtube.com/CigLENy73wIaHwkpW-wd58l8rhMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp"
            },
            "duration":136,
            "viewCount":2,
            "favoriteCount":0,
            "commentCount":0,
            "accessControl":{
                "comment":"allowed",
                "commentVote":"allowed",
                "videoRespond":"moderated",
                "rate":"allowed",
                "embed":"allowed",
                "list":"allowed",
                "autoPlay":"allowed",
                "syndicate":"allowed"
            }
        },
and others items....

モデル クラスをデシリアライズするように設定すると、次のようになりますか?

 public class SearchResponse {

    public Container data;

  }

これは

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;

public class Container {

public ArrayList<Items> results;

@SerializedName("totalItems")
public int totalItems;

@SerializedName("startIndex")
public int startIndex;

@SerializedName("itemsPerPage")
      public int itemsPerPage;
}

そして各アイテムについて:

import com.google.gson.annotations.SerializedName;

public class Items {

@SerializedName("id")
public String id;

@SerializedName("uploaded")
public String uploaded;

@SerializedName("category")
public String category;

@SerializedName("title")
public String title;

@SerializedName("description")
public String description;

public Thumbnail thumbnail;

@SerializedName("duration")
public int duration;


}

および各サムネイルについて

import com.google.gson.annotations.SerializedName;

public class Thumbnail {

@SerializedName("sqdefault")
      public String sqdefault;

@SerializedName("hqdefault")
public String hqdefault;

}

私を殺しているのはその「データ」セクションだと思います。

すでにこれを行っている場合は、誰か助けてください、またはいくつかの例を教えてください。

前もって感謝します

4

2 に答える 2

2

これ:

public ArrayList<Items> results;

する必要があります:

public ArrayList<Items> items;

また:

@SerializedName("items") public ArrayList<Items> results;

そして、それはうまくいくはずです。

いいえ、POJO にすべてのフィールドが存在する必要はありません。JSON に含まれていないフィールドは、黙って無視されます。

于 2012-12-22T16:25:54.363 に答える
1

GSON を使用する必要はありません。自分で行うこともできます。シンプルに保ち、JSON を自分で学習してください :-)

http://blog.blundell-apps.com/show-youtube-user-videos-in-a-listview/

https://github.com/blundell/YouTubeUserFeed

ここに抜粋があります:

 // Create a JSON object that we can use from the String
        JSONObject json = new JSONObject(youTubeJsonResult);

        // Get are search result items
        JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

        // Create a list to store are videos in
        List<Video> videos = new ArrayList<Video>();
        // Loop round our JSON list of videos creating Video objects to use within our app
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // The title of the video
            String title = jsonObject.getString("title");
            // The url link back to YouTube, this checks if it has a mobile url
            // if it doesnt it gets the standard url
            String url;
            try {
                url = jsonObject.getJSONObject("player").getString("mobile");
            } catch (JSONException ignore) {
                url = jsonObject.getJSONObject("player").getString("default");
            }
            // A url to the thumbnail image of the video
            // We will use this later to get an image using a Custom ImageView
            // Found here http://blog.blundell-apps.com/imageview-with-loading-spinner/
            String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");

            // Create the video object and add it to our list
            videos.add(new Video(title, url, thumbUrl));
        }

ビデオは次のとおりです。

public class Video {
    String title;
    String url;
    String thumbUrl
}
于 2012-12-09T20:52:43.423 に答える