微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用json进行GET查询 – Retrofit 2.0

如何进行此查询,我将在下面提到?

方法@GET.查询应如下所示:

?/ TOP40数据= { “排名”: “世界”}
?/ TOP40数据= { “排名”: “国家”}

@GET("/api/top40")
    Call<FamousTop40Model> getStatus(
        // what should be there?
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }

你们能帮助我吗?

编辑:我应该在标题中有accessKey.

解决方法

这有助于我:

public interface FamousTop40Api {
    @GET("/api/top40")
    Call<FamousTop40Model> getStatus(
            @Query("data") String ranking
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                  Request original = chain.request();

                  Request request = original.newBuilder()
                          .header("accessKey",MainActivity.ACCESS_KEY)
                          .method(original.method(),original.body())
                          .build();

                  return chain.proceed(request);
                }
            });

            OkHttpClient client = httpClient.build();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }
}

所以你需要在OkHttpClient的头文件添加@Query和accessKey

FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐