【发布时间】:2016-11-25 09:08:28
【问题描述】:
我正在尝试学习 Retrofit 2.0 并尝试了 http://tutorialwing.com/android-retrofit-library/ 的示例 但是当我浏览教程时,我发现它在说, 基本 url (https://api.stackexchange.com) + 结束 url("/2.2/questions?order=desc&sort=creation&site=stackoverflow"") = 最终 url ("https://api.stackexchange.com/2.2/search?order=desc&sort=activity&tagged=android&site=stackoverflow". ) 但是如何自动附加搜索,尽管它不存在于最终 url。下面是我向 API 发出请求的代码。
public class ApiClient {
public static final String API_BASE_URL = "https://api.stackoverflow.com";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.addInterceptor(interceptor);
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
APIinterface.java
@GET("/2.2/search/{order}/{sort}/{tagged}&site=stackoverflow")
Call<ArrayList<Questions>>loadQuestions(@Query("order")String order, @Query("sort")String sort, @Query("tagged")String tag);
但我没有得到任何结果。放置拦截器后,我能够看到请求的 url 是:https://api.stackoverflow.com/2.2/search/%7Border%7D/%7Bsort%7D/%7Btagged%7D&site=stackoverflow?order=desc&sort=activity&tagged=android,在点击这个 url 后,它会将我重定向到这个 url:https://api.stackexchange.com/docs/api-v1-shutdown?order=desc&sort=activity&tagged=android 我无法找到我出错的地方。任何帮助表示赞赏。
【问题讨论】:
标签: android web-services rest networking retrofit2