改造-呼叫中的参数无效

时间:2019-06-13 16:52:20

标签: java api retrofit

我刚刚从响应中获取代码,并说我的请求参数错误,那么我的api调用应该是什么样?

这是来自documenatation的硬编码api调用

https://api.themoviedb.org/3/discover/movie?api_key=[API_KEY]&with_genres=27

这是我的api调用

@GET("3/search/movie")
Call<itemList_model> test(@Query("api_key") String key,@Query("with_genres") int query);

代码

    Invalid parameters: Your request parameters are incorrect.

改造电话

public void getListViewItems() {
    String url = "https://api.themoviedb.org/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    apiCall api = retrofit.create(apiCall.class);
    Call<itemList_model> call = api.test("API_KEY",27); <- 27 stand's for horror genres.
    call.enqueue(new Callback<itemList_model>() {
        @Override
        public void onResponse(Call<itemList_model> call, Response<itemList_model> response) {
            if (!response.isSuccessful()) {
                Log.i(TAG, "onResponse: " + response.code());
            }
            Log.i(TAG, "onResponse: "+response.code());
        }
        @Override
        public void onFailure(Call<itemList_model> call, Throwable t) {
            Log.i(TAG, "onFailure: " + t.getMessage());
        }
    });
}

1 个答案:

答案 0 :(得分:0)

简单的错字。应该是:

  

https://api.themoviedb.org/3/ 发现 / movie?api_key = [API_KEY]&with_genres = 27

但是:

  

https://api.themoviedb.org/3/ 搜索 / movie?api_key = [API_KEY]&with_genres = 27

工作代码

package test;

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

public interface RetrofitProxy {

    @GET("3/discover/movie")
    Call<Object> test(@Query("api_key") String apiKey, @Query("with_genres") int genreCode);
}


package test;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;

public class RetrofitTest {

    public static void main(String[] args) throws IOException {
        String url = "https://api.themoviedb.org/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitProxy retrofitProxy = retrofit.create(RetrofitProxy.class);
        Call<Object> call = retrofitProxy.test("API_KEY", 27);

        Response<Object> execute = call.execute();
        System.out.println(execute.raw());
        System.out.println(execute.isSuccessful());
        System.out.println(execute.body());
    }
}