Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Map to QueryString [duplicate]

Possible Duplicate:
How to convert map to url query string?

How to convert Map to query string?

For example:

    MockHttpServletRequest request = getRequest();
    HashMap<String, String> queryString = new HashMap<String, String>();
    queryString.put("IDcontent", "4546446");
    queryString.put("ID_CP", "604204d6-3ded-4882-9427-52f1ff8ee6c8");
    request.setQueryString(mapToQueryString(queryString));

I think that is this(mapToQueryString) to do standart library, but I can't find it.

like image 658
zen0n Avatar asked Dec 22 '22 07:12

zen0n


1 Answers

You can define a method mapToueryString by yourself as:

  StringBuilder sb = new StringBuilder();
  for(HashMap.Entry<String, String> e : queryString.entrySet()){
      if(sb.length() > 0){
          sb.append('&');
      }
      sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue(), "UTF-8"));
  }
like image 103
ControlPower Avatar answered Jan 09 '23 21:01

ControlPower