2016-03-17 36 views
2

我需要使用改造從以下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); 
} 
+0

嘗試刪除數組列表的初始值設定項,不要初始化它。僅供參考是一個與MovieDB集成的例子,我作爲項目的一部分。 https://github.com/ramannanda9/MovieDBIntegrationExample –

+0

你必須使自己的自定義JsonDeserializer像解釋[這裏](http://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-使用改造) –

+0

謝謝@RamandeepNanda。你的意思是從我的POJO 1 ..?如果是這樣,只是試了一下,似乎沒有工作。 – Rogerto

回答

3

我不知道爲什麼人們輸入不正確的JSON響應,然後預期問題不會發生。你是否打算髮布整個JSON響應,這是你得到的響應。

{"adult":false,"backdrop_path":"/2ZA03KiD4jePTNBTJjGGFTNQPMA.jpg","belongs_to_collection":null,"budget":102000000,"genres":[{"id":18,"name":"Drama"},{"id":878,"name":"Science Fiction"}],"homepage":"http://cloudatlas.warnerbros.com/","id":83542,"imdb_id":"tt1371111","original_language":"en","original_title":"Cloud Atlas","overview":"A set of six nested stories spanning time between the 19th century and a distant post-apocalyptic future. Cloud Atlas explores how the actions and consequences of individual lives impact one another throughout the past, the present and the future. Action, mystery and romance weave through the story as one soul is shaped from a killer into a hero and a single act of kindness ripples across centuries to inspire a revolution in the distant future. Based on the award winning novel by David Mitchell. Directed by Tom Tykwer and the Wachowskis.","popularity":3.552761,"poster_path":"/8VNiyIp67ZxhpNgdrwACW0jgvP2.jpg","production_companies":[{"name":"Anarchos Productions","id":450},{"name":"X-Filme Creative Pool","id":1972},{"name":"Ascension Pictures","id":7829},{"name":"ARD Degeto Film","id":10947},{"name":"Cloud Atlas Productions","id":11080},{"name":"Five Drops","id":11082},{"name":"Media Asia Group","id":11083},{"name":"Dreams of Dragon Picture","id":19621}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"HK","name":"Hong Kong"},{"iso_3166_1":"SG","name":"Singapore"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"2012-10-26","revenue":130482868,"runtime":172,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"Everything is Connected","title":"Cloud Atlas","video":false,"vote_average":6.4,"vote_count":1774,"reviews":{"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}} 

請注意,您無法直接訪問內部對象。你的外在對象是評論。然後你必須創建一個POJO類來描述它。

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

/** 
* Created by Ramandeep Singh on 17-03-2016. 
*/ 
public class Reviews { 
    @SerializedName("reviews") 
    @Expose 
private MovieReview reviews; 

    public Reviews() { 
    } 


    public MovieReview getReviews() { 
     return reviews; 
    } 

    public void setReviews(MovieReview reviews) { 
     this.reviews = reviews; 
    } 
} 

,此外,該ID不是整數,它是字符串字段。

下次開始輸入正確和完整的JSON響應。

這是方法。修改服務和公用事業

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<Reviews>() { 
         @Override 
         public void onResponse(Call<Reviews> call, Response<Reviews> response) { 
          if (!response.isSuccessful()) { 
           System.out.println("Error"); 
          } 

          reviews = response.body(); 
          movieReview=reviews.getReviews(); 
          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(Call<Reviews> call, Throwable t) { 
          t.printStackTrace(); 

         } 
        } 
    ); 

以後這就是你的服務了。

import retrofit2.Call; 
import retrofit2.http.GET; 
import retrofit2.http.Path; 
import retrofit2.http.Query; 

/** 
* Created by Ramandeep Singh on 17-03-2016. 
*/ 
public class MovieService { 
    public interface MovieDbApi { 

     @GET("/3/movie/{id}?&append_to_response=reviews") 
     Call<Reviews> getReviews(
       @Path("id") int id, 
       @Query("api_key") String apiKey); 

    } 
} 

這是你得到的輸出。

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. 
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. 

Another 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. 

It carried my attention for the 172 minutes well enough and it was entertaining. I just expected more of a pay off at the end. 

All 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? 
+0

@pratt你檢查了JSON響應我的完成他不是。有不同的字段名爲id。我們感興趣的是字符串。在發佈之前請檢查一下。 –

0

試試這個,而不是分開打電話和打電話與enqueue。

call = movieDbApi.getReviews(id, API_KEY).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()); 

     } 
    }); 
+0

這似乎沒有工作 – Rogerto

+0

你能否在日誌文件中正確得到響應? – HourGlass