【问题标题】:url construction in Retrofit 2.0Retrofit 2.0 中的 url 构造
【发布时间】: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


    【解决方案1】:

    ordersorttagged 是 url 的 Path 的一部分,因此您需要为此使用 @Path 注释。唯一的查询是site=stackoverflow

    @GET("/2.2/search/{order}/{sort}/{tagged}&site=stackoverflow")
    Call<ArrayList<Questions>>loadQuestions(@Path("order")String order, @Path("sort")String sort, @Path("tagged")String tag, @Query("site") siteName);
    

    应该输入你想要的 url。

    编辑:如果你想查询https://api.stackexchange.com/2.2/search?order=desc&amp;sort=activity&amp;tagged=android&amp;site=stackoverflow 那么你的定义应该是

     @GET("/2.2/search")
     Call<ArrayList<Questions>>loadQuestions(@Query("order")String order, @Query("sort")String sort, @Query("tagged")String tag, @Query("site") siteName);
    

    或者您也可以使用@QueryMap 提供单个参数

    【讨论】:

    • 它仍然重定向到api.stackexchange.com/docs/… 并且通过查看 url 排序,order & tagged 不是查询参数吗?因为我们必须为此指定值。
    • 你必须决定它是否查询路径。他们是两个不同的东西。你猜不到。你有你要查询的端点的定义吗?
    • @HardikMehta 不,? 后面的任何内容都是查询字符串。查看URI generic syntax 以获得更好的理解。
    • @Blackbelt 它是开放的 api api.stackexchange.com/2.2/… 。我想知道为什么要附加 http/1.1
    • @Blackbelt 你忘了字符串站点名称吗?或者应该和你写的一样
    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2016-01-13
    • 1970-01-01
    • 2017-03-24
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多