2013-09-25 26 views
1

首先,我是JSON和GSON的初學者,請耐心等待。如何在Android應用中使用GSON從Youtube API讀取JSON-c數據

我想讀,我從這個鏈接中獲取的數據:

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

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

但是我不能檢索數據。 this.library變量,即Library Class的實例始終爲空。

任何幫助表示讚賞,請問我是否需要更多的代碼。

非常感謝

回答

2

OK,我能理解你沒有任何關於JSON和GSON任何想法......但你們採取了至少一個快速瀏覽一下,以JSON specificationsGson documentation

在閱讀了一段時間後,我建議您使用這個方便的在線JSON Viewer以用戶友好的方式查看您正在檢索的JSON數據。你只需要整個JSON複製文本選項卡並單擊查看器選項卡...

如果你這樣做,你會看到你的JSON的結構,這是這樣的:

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

現在,你需要做的是創建一個代表JSON數據結構的Java類結構,而你並沒有這樣做!爲什麼你將uservideos的屬性添加到你的Library類中?您是否認爲Gson可以神奇地知道您想要檢索用戶和他的視頻?它並不真的這樣做......

爲了創建合適的類結構,通過做這樣的事情(在僞代碼)開始:

class Response 
    Data data; 

class Data 
    List<Item> items; 

class Item 
    String id; 
    String title; 

因爲你可能現在已經實現了,這個類結構確實代表了你的JSON數據!然後根據要檢索的數據添加更多的類和屬性(只添加需要檢索的類和屬性,剩下的將自動跳過)。

+0

是的,我已經做了JSON唯一使用json_encode在PHP中:d 非常感謝您的幫助,我真的很感激。 –

+0

@BlazeTama,沒問題,我很高興它幫助!你會發現只需一點練習就很容易 – MikO

+0

感謝你,現在我終於可以檢索這些數據。但是,當我想要使用這些數據時,我遇到了問題。請看看:http:// stackoverflow。com/questions/19026759/how-to-get-variables-value-from-gson如果你有空閒時間。感謝:D –

相關問題