使用标题和参数更改URL

时间:2016-07-19 19:46:44

标签: android retrofit2

我在使用Retrofit 2库时遇到了一些麻烦。我想用https请求发送标题和参数,我从日志中得到以下网址:

https://api.trakt.tv/movies/popular(page= '1')限制= 10&安培;扩展=完全,图像

我收到404状态代码。上面的网址是对的吗?

字符串ENDPOINT =“https://api.trakt.tv/”;

@GET("movies/popular(page='{page}')?limit=10&extended=full,images")
public Observable<PopularMoviesResponse> getPopularMovies(@Header("trakt-api-version") String trakt_api_version,
                                                  @Header("trakt-api-key") String trakt_api_key,
                                                  @Path("page") String page);

/********
 * Helper class that sets up a new services
 *******/
class Creator {

    public static MovieService newSurveiesService() {
        Gson gson = new GsonBuilder()
                .setDateFormat("dd/MM/yyyy hh:mm:ss")
                .create();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MovieService.ENDPOINT)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();


        return retrofit.create(MovieService.class);
    }
}

1 个答案:

答案 0 :(得分:0)

看起来@GET中的您的网址是错误的,基于您说网址@CaseyB提供的功能。

@Get("movies/popular?page={page}&limit=10&extended=full,images")

或者,您可以使用@Query注释让Retrofit为您处理查询字符串:

@Get("movies/popular")
public Observable<PopularMoviesResponse> getPopularMovies(
                                              @Header("trakt-api-version") String trakt_api_version,
                                              @Header("trakt-api-key") String trakt_api_key,
                                              @Query("page") int page,
                                              @Query("limit") int limit,
                                              @Query("extended") String extended);