我需要使用改造從以下API查詢評價的列表:改造 - 如何從JSON獲得響應對象
http://api.themoviedb.org/3/movie/{id}/reviews?api_key=#######
的JSON返回的是:
{"id":83542,"page":1,"results":[{"id":"51910979760ee320eb020fc2","author":"Andres Gomez","content":"Interesting film with an exceptional cast, fantastic performances and characterizations. The story, though, is a bit difficult to follow and, in the end, seems to not have a real point.","url":"https://www.themoviedb.org/review/51910979760ee320eb020fc2"},{"id":"520a8d10760ee32c8718e6c2","author":"Travis Bell","content":"Cloud Atlas was a very well made movie but unlike most of the \"simultaneous stories that all come together at the end\" type of movies, this one just didn't. I'm still unclear as to the point of it all.\r\n\r\nAnother issue I had was a general feeling of goofiness. Sure, the Cavendish story was pure comedy but the rest of the stories just didn't feel serious enough to me.\r\n\r\nIt carried my attention for the 172 minutes well enough and it was entertaining. I just expected more of a pay off at the end.\r\n\r\nAll in all, it's definitely worth seeing but I still haven't made up my mind if I truly liked it or not. What did you think?","url":"https://www.themoviedb.org/review/520a8d10760ee32c8718e6c2"}],"total_pages":1,"total_results":2}
我的目標是以訪問「內容」屬性,然後放入一個TextView中。
我遇到麻煩的是,出於某種原因,我無法弄清楚如何從上面的JSON返回結果對象列表,然後使用getContent()訪問content屬性。
我認爲我使用模型對象的方式可能存在問題?它們應該是兩個獨立的對象嗎?我不確定。當我嘗試打印對象時,我會得到空的答覆...
任何幫助都會很棒。
改造接口代碼是:
public class MovieService {
public interface MovieDbApi {
@GET("/3/movie/{id}?&append_to_response=reviews")
Call<MovieReview> getReviews(
@Path("id") int id,
@Query("api_key") String apiKey);
}
}
模型對象的代碼是:
POJO 1
public class MovieReview {
@SerializedName("id")
@Expose
private int id;
@SerializedName("page")
@Expose
private int page;
@SerializedName("results")
@Expose
private List<MovieReviewDetail> results = new ArrayList<MovieReviewDetail>();
@SerializedName("total_pages")
@Expose
private int totalPages;
@SerializedName("total_results")
@Expose
private int totalResults;
/**
*
* @return
* The id
*/
public int getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(int id) {
this.id = id;
}
/**
*
* @return
* The page
*/
public int getPage() {
return page;
}
/**
*
* @param page
* The page
*/
public void setPage(int page) {
this.page = page;
}
/**
*
* @return
* The results
*/
public List<MovieReviewDetail> getResults() {
return results;
}
/**
*
* @param results
* The results
*/
public void setResults(List<MovieReviewDetail> results) {
this.results = results;
}
/**
*
* @return
* The totalPages
*/
public int getTotalPages() {
return totalPages;
}
/**
*
* @param totalPages
* The total_pages
*/
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
/**
*
* @return
* The totalResults
*/
public int getTotalResults() {
return totalResults;
}
/**
*
* @param totalResults
* The total_results
*/
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
}
POJO 2
public class MovieReviewDetail {
@SerializedName("id")
@Expose
private int id;
@SerializedName("author")
@Expose
private String author;
@SerializedName("content")
@Expose
private String content;
@SerializedName("url")
@Expose
private String url;
/**
*
* @return
* The id
*/
public int getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(int id) {
this.id = id;
}
/**
*
* @return
* The author
*/
public String getAuthor() {
return author;
}
/**
*
* @param author
* The author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
*
* @return
* The content
*/
public String getContent() {
return content;
}
/**
*
* @param content
* The content
*/
public void setContent(String content) {
this.content = content;
}
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}
}
片段代碼:
private static final String API_KEY = "#####";
private static final String API_BASE_URL_MOVIES = "http://api.themoviedb.org/";
private Call<MovieReview> call;
private MovieReview movieReview;
private List <MovieReviewDetail> movieReviewDetails;
public void getMovieReview(int id){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL_MOVIES)
.addConverterFactory(GsonConverterFactory.create())
.build();
MovieService.MovieDbApi movieDbApi = retrofit.create(MovieService.MovieDbApi.class);
call = movieDbApi.getReviews(id, API_KEY);
call.enqueue(new Callback<MovieReview>() {
@Override
public void onResponse(Response<MovieReview> response) {
if (!response.isSuccess()) {
Log.d(LOG_TAG, "Error");
}
movieReview = response.body();
movieReviewDetails = movieReview.getResults();
//Print out results
for (int i = 0; i < movieReviewDetails.size(); i++) {
System.out.println(movieReviewDetails.get(i).getContent());
}
}
@Override
public void onFailure(Throwable t) {
Log.e("Throwable: ", t.getMessage());
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getMovieReview(83542);
}
嘗試刪除數組列表的初始值設定項,不要初始化它。僅供參考是一個與MovieDB集成的例子,我作爲項目的一部分。 https://github.com/ramannanda9/MovieDBIntegrationExample –
你必須使自己的自定義JsonDeserializer像解釋[這裏](http://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-使用改造) –
謝謝@RamandeepNanda。你的意思是從我的POJO 1 ..?如果是這樣,只是試了一下,似乎沒有工作。 – Rogerto