如何使用Retrofit2进行查询数组请求?

时间:2016-10-04 19:20:05

标签: android http retrofit retrofit2 okhttp

我需要使用此查询发出GET请求:

.../apartments?category[first][one]=53&category[first][two]=27&category[second][one]=53&category[second][two]=27&order=created_at&page=1

我试着像这样添加它:

    public interface ApiService {

        @GET(SEARCH + CATEGORY)
        Observable<Responce> getResponceObservable(
                @Query("category[first][one]") double var1,
                @Query("category[first][two]") double var2,
                @Query("category[second][one]") double var3,
                @Query("category[second][two]") double var4,
                @Query("order") String order,
                @Query("page") int page
        );

但是日志错误如下:

<-- 415 Unsupported Media Type https://base.com/search/categories?category[first][one]=53&category[first][two]=53&category[second][one]=53&category[second][two]=53&order=created_at&page=1 (384ms)

1 个答案:

答案 0 :(得分:0)

为什么不用@Body发送数据:

它会是这样的:

public class Category {

double category1,category2,category3,category4;
String order;
int page;

public double getCategory1() {
    return category1;
}

public void setCategory1(double category1) {
    this.category1 = category1;
}

public double getCategory2() {
    return category2;
}

public void setCategory2(double category2) {
    this.category2 = category2;
}

public double getCategory3() {
    return category3;
}

public void setCategory3(double category3) {
    this.category3 = category3;
}

public double getCategory4() {
    return category4;
}

public void setCategory4(double category4) {
    this.category4 = category4;
}

public String getOrder() {
    return order;
}

public void setOrder(String order) {
    this.order = order;
}

public int getPage() {
    return page;
}

public void setPage(int page) {
    this.page = page;
}

}

要发送数据,请执行以下操作:

Category category = new category();
... //Set your data

@GET("apartments")
Call<CategoryResponse> send_data(@Body Category category );

请参阅以下链接以供参考: https://futurestud.io/tutorials/retrofit-send-objects-in-request-body