【问题标题】:How to make Query array request with Retrofit2?如何使用 Retrofit2 进行查询数组请求?
【发布时间】:2017-02-13 01:59:23
【问题描述】:

我需要用这个查询发出 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)

【问题讨论】:

    标签: android http retrofit retrofit2 okhttp


    【解决方案1】:

    为什么不用@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

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2019-10-05
      • 2018-02-13
      • 1970-01-01
      • 2017-09-30
      • 2019-08-21
      相关资源
      最近更新 更多