我正在使用retrofit 2.0从Youtube API地址获取GSON:
Link (temporarily static) for 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
);
}
这是我的主要活动:
@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());
}
});
当我运行我的代码时,它向我显示“Works!”,但我尝试了几种方法来获取videoID但没有成功! 我做错了什么?
答案 0 :(得分:0)
你正在制作一个错误的模型类。尝试以下
画
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;
}
}
RESOURCEID
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();