2016-10-04 70 views
1

我需要GET請求與此查詢:如何使用Retrofit2創建查詢數組請求?

.../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) 

回答

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