我正在尝试调用一个需要传递API密钥的API。
我的服务调用使用HttpURLConnection
已经完美运行。
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());
}
然而,我不确定这如何与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);
我正在使用这个来调用它
Call<String> call = endpoint.getRestaurantsBySearch("3","city","mumbai","application/json","9900a9720d31dfd5fdb4352700c");
所有这些调用都进入了RetroFit中的OnFailure
方法。
如果我不发送HeaderParameters,它会因为403而成功,因为我显然需要在某个地方传递API密钥,但我无法弄清楚如何传递。@GET("search")
Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
我在OnFailure中收到的错误是什么。java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $