问题描述
我正在尝试调用一个API,该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 $
尝试了几次之后,我找到了答案.
错误
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
由于解析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);
我应该传递的呼叫类型应该不是Call< String >
数据属于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);
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("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());
}
However, I'm not sure how this works with RetroFit as my call in going into Failure at all times.Heres the code I'm using for the same service call
@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);
and I'm using this to call it
Call<String> call = endpoint.getRestaurantsBySearch("3","city","mumbai","application/json","9900a9720d31dfd5fdb4352700c");
All these calls are going into the OnFailure Method in RetroFit.If I send it without the HeaderParameters it goes into Success with a 403 because I obviously need to pass the api key somewhere but I cant figure out how.
@GET("search")
Call<String> getRestaurantsBySearch(@Query("entity_id") String entity_id, @Query("entity_type") String entity_type, @Query("q") String query);
The error I'm getting in OnFailure is
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
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);
这篇关于在翻新中添加标题参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!