1

まず、JSON と GSON の初心者なので、ご容赦ください。

このリンクから取得したデータを読みたい:

https://gdata.youtube.com/feeds/api/videos?author=radityadika&v=2&alt=jsonc

だから私は上記のリンクの結果を表すいくつかのクラスを作成しようとしました:

ビデオ.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;
    }
}

Library.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);
        }
    }

ただし、データを取得できません。常に nullのthis.libraryインスタンスである変数。Library Class

さらにコードが必要な場合は、私に尋ねてください。

どうもありがとう

4

1 に答える 1

2

わかりました。あなたが JSON と Gson について何も知らないのは理解できますが、JSON の仕様Gson のドキュメントをざっと見たことがありますか?

少し読んだ後、この便利なオンラインJSON ビューアーを使用して、取得している JSON データをユーザーフレンドリーな方法で表示することをお勧めします。[テキスト] タブで JSON 全体をコピーし、[ビューア] タブをクリックするだけです...

これを行うと、次のような JSON の構造が表示されます。

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

さて、あなたがしなければならないことは、JSON のデータ構造を表す Java クラス構造を作成することですが、あなたはそれをしていません! なぜ属性userをクラスvideosに追加したのですか? LibraryGson は、あなたがユーザーと彼の動画を取得したいことを魔法のように理解できると思いましたか? なかなかうまくいかない……。

適切なクラス構造を作成するには、次のようなことから始めます (疑似コードで)。

class Response
  Data data;

class Data
  List<Item> items;

class Item
  String id;
  String title;

もうお気づきかもしれませんが、このクラス構造は JSON データを表しているからです。次に、取得するデータに応じてクラスと属性を追加します (取得する必要があるクラスと属性のみを追加すると、残りは自動的にスキップされます)。

于 2013-09-25T16:03:38.210 に答える