代码之家  ›  专栏  ›  技术社区  ›  Andre

获取响应时出错。正文并转换为字符串

  •  0
  • Andre  · 技术社区  · 7 年前

    Iam使用改型2.0从Youtube API地址获取GSON:

    Link (temporarily static) for Youtube Address

    GSON from Youtube address

    但是当我试图从回复中获取视频ID时。身体它不工作!

    我的代码:

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

    这里是我的主要活动:

    @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,但都没有成功! 我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Munir    7 年前

    你把模型课弄错了。在下面尝试

    视频

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