Adding duplicate options with Retrofit v1.9.0

Here a similar question is asked here , but my case is slightly different.

I am trying to make a query similar to the following:

http://www.example.com/abc?foo=def&foo=ghi&foo=jkl&bar=xyz

I have two problems that make work difficult. Firstly, a repeating parameter (setting values ​​for "foo" several times) prevents use QueryMap(I have no way to pass values ​​in the query string differently, for example, as an array). Secondly, the query parameters that I use are kind of dynamic, so I cannot use Queryand provide it with a list of values ​​for the name of this parameter, since I will not know the names of the parameters until I create the request.

The code I'm trying to update uses the old version of Retrofit, but for some reason has a concept QueryListthat took Listof NameValuePairfor passing in the query parameters as a name (and their values ​​as values) and allowing duplicate parameters. I don’t see links to retrofit.http.QueryListanywhere in the Source Code History for Retrofit or the Internet, so I'm not sure if this was a custom add-on at the time. In any case, I'm trying to find the best way to reproduce this functionality in the latest version so that any help is appreciated!

+3
source share
2 answers

, QueryMap . List NameValuePair HashMap, , , . , (, ) (key, value & key = value2). , , = & key = value2 . , , , , , .

, HashMap List :

public static HashMap<String, String> getPathMap(List<NameValuePair> params) {
    HashMap<String, String> paramMap = new HashMap<>();
    String paramValue;

    for (NameValuePair paramPair : params) {
        if (!TextUtils.isEmpty(paramPair.getName())) {
            try {
                if (paramMap.containsKey(paramPair.getName())) {
                    // Add the duplicate key and new value onto the previous value
                    // so (key, value) will now look like (key, value&key=value2)
                    // which is a hack to work with Retrofit QueryMap
                    paramValue = paramMap.get(paramPair.getName());
                    paramValue += "&" + paramPair.getName() + "=" + URLEncoder.encode(String.valueOf(paramPair.getValue()), "UTF-8");
                } else {
                    // This is the first value, so directly map it
                    paramValue = URLEncoder.encode(String.valueOf(paramPair.getValue()), "UTF-8");
                }
                paramMap.put(paramPair.getName(), paramValue);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    return paramMap;
}

:

@GET("/api/")
List<Repo> listRepos(@QueryMap(encodeValues=false) Map<String, String> params);

:

// Get the list of params for the service call
ArrayList<NameValuePair> paramList = getParams();

// Convert the list into a map and make the call with it
Map<String, String> params = getPathMap(paramList);
List<Repo> repos = service.listRepos(params);

Path, , , QueryMap. , , !

+16

@Query:

@GET("/api")
Response shareItem(@Query("email")List<String> email);
-1

Source: https://habr.com/ru/post/1695954/


All Articles