Created
October 6, 2016 21:35
-
-
Save valdio/f3129f8c7da85838b9c04e6f1bb8d498 to your computer and use it in GitHub Desktop.
Retrofit Multiple Query Parameter with custom QueryMap .... Example: ?tag[5]=12&tag[20]=3&tag[11]=5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ApiEndpointInterface { | |
//Example of the implementation | |
@FormUrlEncoded | |
@POST("{token}/pay/cash?sandbox=1") | |
Call<ResponseBody> orderProducts(@Path("token") String token, | |
@QueryMap Map<String, String> map, | |
@Field("address") String addressId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by Valdio Veliu on 10/6/2016. | |
*/ | |
public class RetrofitMapEntry implements Map.Entry<List<String>, List<String>> { | |
private List<String> key; | |
private List<String> value; | |
public RetrofitMapEntry(List<String> key, List<String> value) { | |
this.key = key; | |
this.value = value; | |
} | |
@Override | |
public List<String> getKey() { | |
return key; | |
} | |
@Override | |
public List<String> getValue() { | |
return value; | |
} | |
@Override | |
public List<String> setValue(List<String> value) { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map.Entry; | |
/** | |
* Created by Valdio Veliu on 10/6/2016. | |
*/ | |
public class RetrofitQueryMapBuilder { | |
public LinkedHashMap<String, String> customHashMap(String queryKey, Entry<List<String>, List<String>> entry) { | |
LinkedHashMap<String, String> map = new LinkedHashMap<>(); | |
String key = queryKey; | |
List<String> keyList = entry.getKey(); | |
List<String> valueList = entry.getValue(); | |
//key and value lists must have the same length | |
if (keyList == null || valueList == null) return null; | |
if (keyList.size() != valueList.size()) return null; | |
int listSize = valueList.size(); | |
for (int i = 0; i < listSize; i++) { | |
map.put(key + "[" + keyList.get(i) + "]", valueList.get(i)); | |
} | |
return map; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeClass { | |
//... | |
private void makeNerworkCall() { | |
Call<ResponseBody> networkCall =...; | |
//Fill the Lists with some data | |
//Their length should be the same | |
List<String> keys = new ArrayList<>(); | |
List<String> values = new ArrayList<>(); | |
//... Fill with data | |
//Build the LinkedHashMap | |
String KEY_TAG = "tag"; //Specify the String TAG | |
Map.Entry<List<String>, List<String>> entry = new RetrofitMapEntry(keys, values); | |
LinkedHashMap<String, String> map = new RetrofitQueryMapBuilder().customHashMap(KEY_TAG, entry); | |
networkCall = apiService.orderProducts("TOKEN_STRING", map, "addressId"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if i need to send my parameter is Example: ?tag[5]=12,45,34 ?