【问题标题】:Error getting response.body and converting to String获取 response.body 并转换为 String 时出错
【发布时间】:2017-11-06 01:13:02
【问题描述】:

我正在使用改造 2.0 从 Youtube API 地址获取 GSON:

Link (temporarily static) for Youtube Address

GSON from Youtube address

但是当我尝试从 response.body 获取 videoID 时,它不起作用!

我的代码:

public class Videos {


    @com.google.gson.annotations.SerializedName("videoId") String mVideoId;
    @com.google.gson.annotations.SerializedName("title") String mTitle;

   public Videos(String videoId, String title ) {
        this.mVideoId = videoId;
        this.mTitle = title;
    }

    public String getmVideoId() {
        return mVideoId;
    }
    public void setmVideoId(String mVideoId) {
        this.mVideoId = mVideoId;
    }
    public String getmTitle() {
        return mTitle;
    }
    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

}

我的界面:

public interface YoutubeApiURL {

@GET("youtube/v3/playlistItems")
Call<Videos> listVideos (
    @Query("part") String part,
    @Query("maxResults") String maxResults,
    @Query("playlistId") String playlistId,
    @Query("fields") String fields,
    @Query("key") String key
);
}

这里是我的 MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.btn_play);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtube_player_view);
    btnListar = (Button)findViewById(R.id.btnListaGitHub);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://www.googleapis.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    YoutubeApiURL service = retrofit.create(YoutubeApiURL.class);


    Call<Videos>  videos = service.listVideos(
            "snippet",
            "50",
            "PLbZ3V_t0ZLzylEoYuPtmT5uArAzMewz-m",
            "items/snippet/resourceId/videoId",
            "AIzaSyAH2YDhp_Yle3NhLeCuBqH654lUre4vDHw");

    myURL = videos.request().url().toString();
    Log.i("ListVideos", myURL);


    videos.enqueue(new Callback<Videos>() {
        @Override
        public void onResponse(Call<Videos> call, Response<Videos> response) {

            if (response.isSuccessful()) {
                Log.i("ListVideos","Works!");
                // Here some code to show de videoIds return
            } else {
                Log.i("ListVideos","Something wrong");
            }

        }

        @Override
        public void onFailure(Call<Videos> call, Throwable t) {
            Log.i("ListVideos: ", t.getLocalizedMessage());
        }
    });

当我运行我的代码时,它向我显示“有效!”,但我尝试了多种方法来获取 videoID,但均未成功! 我做错了什么?

【问题讨论】:

    标签: android youtube youtube-api retrofit2


    【解决方案1】:

    你把模型类弄错了。试试下面

    视频

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    import java.util.List;
    
    public class Videos {
    
    @SerializedName("items")
    @Expose
    private List<Item> items = null;
    
    public List<Item> getItems() {
        return items;
    }
    
    public void setItems(List<Item> items) {
        this.items = items;
    }
    
    public Videos withItems(List<Item> items) {
        this.items = items;
        return this;
    }
    
    }
    

    片段

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Snippet {
    
    @SerializedName("resourceId")
    @Expose
    private ResourceId resourceId;
    
    public ResourceId getResourceId() {
        return resourceId;
    }
    
    public void setResourceId(ResourceId resourceId) {
        this.resourceId = resourceId;
    }
    
    public Snippet withResourceId(ResourceId resourceId) {
        this.resourceId = resourceId;
        return this;
    }
    
    }
    

    资源ID

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class ResourceId {
    
    @SerializedName("videoId")
    @Expose
    private String videoId;
    
    public String getVideoId() {
        return videoId;
    }
    
    public void setVideoId(String videoId) {
        this.videoId = videoId;
    }
    
    public ResourceId withVideoId(String videoId) {
        this.videoId = videoId;
        return this;
    }
    
    }
    

    物品

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Item {
    
    @SerializedName("snippet")
    @Expose
    private Snippet snippet;
    
    public Snippet getSnippet() {
        return snippet;
    }
    
    public void setSnippet(Snippet snippet) {
        this.snippet = snippet;
    }
    
    public Item withSnippet(Snippet snippet) {
        this.snippet = snippet;
        return this;
    }
    
    }
    

    现在在成功方法中使用下面的代码获取视频ID

    response.body().getItems().get(0).getSnippet().getResourceId().getVideoId();
    

    【讨论】:

    • @Andre 很高兴为您提供帮助,如果答案对您有所帮助,请不要忘记接受它作为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多