2017-08-07 44 views
1

這是我的接口:改造 - 發送動態查詢與相同的密鑰

public interface ServerCalls{ 
     @GET 
     Call<List<Integer>> searchNames(@Url String url, @QueryMap Map<String, 
     String> options); 
    } 
} 

,這是我的電話:

APIs.LoadDataService service = 
     retrofit.create(APIs.LoadDataService.class); 
     Map<String, String> parameters = new HashMap<>(); 
     parameters.put("name","yoni"); 
     parameters.put("name","albert"); 
     parameters.put("q","text"); 
     Call<List<Integer>> call = service.searchNames(APIs.GET_NAMES, parameters); 

,以與動態查詢我需要使用@QueryMap這樣一個電話意味着我需要使用一些地圖。 我的問題是當我需要發送相同的密鑰時,因爲Map始終獲得來自密鑰的最後一個值而不支持重複。

我的API需要得到類似的東西:

myapi.com/getLastName?name=yoni&name=albert 

我該怎麼辦呢? (不含番石榴文庫)

+0

你不能改變API嗎?這是奇怪的設計,如果它需要一個名稱列表,那麼它應該是Body中的一個數組。 –

+0

[Retrofit v1.9.0添加重複參數]可能的重複(https://stackoverflow.com/questions/28776381/adding-duplicate-parameters-with-retrofit-v1-9-0) –

回答

0

您可以使用@Query("name") List<String> names

示例:this鏈路

public interface Api { 
    @GET("/getLastName") 
    Call<ResponseBody> getLastName(@Query("name") List<String> names, 
            @QueryMap Map<String, String> otherUniqueKeys); 
} 

List<String> names = new ArrayList(); 
names.add("yoni"); 
names.add("albert"); 

// create an instance of Api interface with retrofit 
Api api = ... 
api.getLastName(names).enqueue(new Callback<ResponseBody>() { 
    @Override 
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 

    } 

    @Override 
    public void onFailure(Call<ResponseBody> call, Throwable t) { 

    } 
}); 

的更多細節。

+0

這就是我所做的。 .. – Yoni

+0

@Yoni我更新了我的答案。 –

+0

但這樣做的輸出是: name = yoni,albert 我需要: name = yoni&name = albert – Yoni

0

這樣

@POST 
Call<Response> dynamicCall(
     @Url String url, 
     @Body Object object); 

使用製作接口:

dynamicCall(url,Object); 

我希望這會幫助你。