在Retrofit中添加标题参数

jam*_*ian 23 android httpurlconnection retrofit

我正在尝试调用api,这需要我传入一个API密钥.

使用HtppURLconnection进行的Sercive调用非常有效.

url = new URL("https://developers.zomato.com/api/v2.1/search?entity_id=3&entity_type=city&q=" + params[0]);
        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestProperty("user-key","9900a9720d31dfd5fdb4352700c");

        if (urlConnection.getResponseCode() != 200) {
            Toast.makeText(con, "url connection response not 200 | " + urlConnection.getResponseCode(), Toast.LENGTH_SHORT).show();
            Log.d("jamian", "url connection response not 200 | " + urlConnection.getResponseCode());
            throw new RuntimeException("Failed : HTTP error code : " + urlConnection.getResponseCode());
        }
Run Code Online (Sandbox Code Playgroud)

然而,我不确定这是如何与RetroFit一起工作的,因为我始终要进入失败状态.下面是我用于同一服务调用的代码

 @GET("search")
Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query,@Header("Accept") String accept, @Header("user-key") String userkey);
Run Code Online (Sandbox Code Playgroud)

而我正在用它来称呼它

Call<String> call = endpoint.getRestaurantsBySearch("3","city","mumbai","application/json","9900a9720d31dfd5fdb4352700c");
Run Code Online (Sandbox Code Playgroud)

所有这些调用都进入了RetroFit中的OnFailure方法.如果我在没有HeaderParameters的情况下发送它,它会因为403而进入Success成功,我显然需要在某处传递api密钥,但我无法弄清楚如何.

@GET("search")
Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
Run Code Online (Sandbox Code Playgroud)

我在OnFailure中遇到的错误是

java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 38

您可以使用以下

 @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);
Run Code Online (Sandbox Code Playgroud)

 Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes");
Run Code Online (Sandbox Code Playgroud)

以上是基于zomato api的文档

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

需要注意的是终点改变api/v2.1/search和Header @Headers("user-key: 9900a9720d31dfd5fdb4352700c").

还要检查你的基本网址 .baseUrl("https://developers.zomato.com/")

此外,我尝试了上面的api键我生成,它的工作原理和我的查询是咖啡馆建议zomato文档.

注意:我希望你有以下内容

 .addConverterFactory(ScalarsConverterFactory.create()) // for string conversion
 .build();
Run Code Online (Sandbox Code Playgroud)

以及build.gradle文件中的以下内容

compile group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.2.0'
Run Code Online (Sandbox Code Playgroud)

编辑:

您还可以使用动态值传递标头,如下所示

@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);
Run Code Online (Sandbox Code Playgroud)

Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes","9900a9720d31dfd5fdb4352700c");
Run Code Online (Sandbox Code Playgroud)


Avi*_*rma 28

试试Retrofit 1.9和2.0的这种类型标题.对于Json内容类型.

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

你可以添加更多标题,即

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


jam*_*ian 6

尝试了几次后,我想出了答案。

错误

java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
Run Code Online (Sandbox Code Playgroud)

由于解析json失败而即将到来。

在方法调用中,我传递的是 String 而不是 POJO 类。

@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);
Run Code Online (Sandbox Code Playgroud)

我应该传递而不是 Call< String > 的类型 Call< Data >

数据是 Pojo 类

像这样的东西

@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);
Run Code Online (Sandbox Code Playgroud)