Add Header Parameter in Retrofit

前端 未结 5 1009
抹茶落季
抹茶落季 2020-12-28 12:44

I\'m trying to call an API which requires me to pass in an API key.

My Service call using HttpURLConnection is working perfectly.

url = new URL(\"htt         


        
相关标签:
5条回答
  • 2020-12-28 13:02

    After trying a couple of times i figured out the answer.

    The error

    java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
    

    was coming due the failure of parsing the json.

    In the method call I was passing a String instead of a POJO class.

    @Headers("user-key: 9900a9720d31dfd5fdb4352700c")
    @GET("api/v2.1/search")
    Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
    

    I should have passed instead of Call<String> the type of Call<Data>

    Data being the Pojo class

    something like this

    @Headers("user-key: 9900a9720d31dfd5fdb4352700c")
    @GET("api/v2.1/search")
    Call<Data> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
    
    0 讨论(0)
  • 2020-12-28 13:04

    Try this type header for Retrofit 1.9 and 2.0. For Json Content Type.

    @Headers({"Accept: application/json"})
    @POST("user/classes")
    Call<playlist> addToPlaylist(@Body PlaylistParm parm);
    

    You can add many more headers i.e

    @Headers({
            "Accept: application/json",
            "User-Agent: Your-App-Name",
            "Cache-Control: max-age=640000"
        })
    

    Dynamically Add to headers:

    @POST("user/classes")
    Call<ResponseModel> addToPlaylist(@Header("Content-Type") String content_type, @Body RequestModel req);
    

    Call you method i.e

    mAPI.addToPlayList("application/json", playListParam);
    

    Or

    Want to pass everytime then Create HttpClient object with http Interceptor:

    OkHttpClient httpClient = new OkHttpClient();
            httpClient.networkInterceptors().add(new Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                    Request.Builder requestBuilder = chain.request().newBuilder();
                    requestBuilder.header("Content-Type", "application/json");
                    return chain.proceed(requestBuilder.build());
                }
            });
    

    Then add to retrofit object

    Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(httpClient).build();
    

    UPDATE if you are using Kotlin remove the { } else it will not work

    0 讨论(0)
  • 2020-12-28 13:07

    You can use the below

     @Headers("user-key: 9900a9720d31dfd5fdb4352700c")
     @GET("api/v2.1/search")
     Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
    

    and

     Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes");
    

    The above is based in the zomato api which is documented at

    https://developers.zomato.com/documentation#!/restaurant/search

    Thing to note is the end point change api/v2.1/search and the Header @Headers("user-key: 9900a9720d31dfd5fdb4352700c").

    Also check your base url .baseUrl("https://developers.zomato.com/")

    Also i tried the above with a api key i generated and it works and my query was cafes as suggested the zomato documentation.

    Note : I hope you have the below

     .addConverterFactory(ScalarsConverterFactory.create()) // for string conversion
     .build();
    

    and the below in build.gradle file

    compile group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.2.0'
    

    Edit:

    You can also pass header with dynamic value as below

    @GET("api/v2.1/search")
    Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query,@Header("user-key") String userkey);
    

    And

    Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes","9900a9720d31dfd5fdb4352700c");
    
    0 讨论(0)
  • 2020-12-28 13:08

    Please take a look at the response. It clearly shows that the api key you provided is wrong. At first you get the correct api key. Then call the request it will work .

    0 讨论(0)
  • 2020-12-28 13:19

    As far as i can see you are passing the data in a wrong way. Your method getRestaurantsBySearch is accepting the last two parameter as header field i.e accept and user-key. But while calling the method you are passing headers first. Pass the data as you have declared it in method signature of getRestaurantsBySearch

    0 讨论(0)
提交回复
热议问题