android - Escaping reserved url parameters in Java -


i building android app , there part of app need post url form data. 1 of form fields pass along email address.

i noticed issue email addresses have '+' sign in them reserved character in urls means ' '. wanted know, how can sanitize/escape characters , others in code before convert post byte[]. don't want replaceall. there specific encoder built java this?

here code use:

stringbuilder builder = new stringbuilder(); builder.append(id + "=" + params.id + "&"); builder.append(locale + "=" + params.locale + "&"); builder.append(email + "=" + params.getemail());  string encodedparams = builder.tostring(); mwebview.posturl(url, encodingutils.getasciibytes(encodedparams)); 

try using java.net.urlencoder.encode(valuetoencode, "utf-8");

it's been while since i've looked @ details, believe have call encode() on individual parts of string before concatenate them.

the utility method below has been working me:

    /**      * given {@link map} of keys , values, method return string      * represents key-value pairs in      * 'application/x-www-form-urlencoded' mime format.      *       * @param keysandvalues      *            keys , values      * @return data in 'application/x-www-form-urlencoded' mime format      */     private string wwwformurlencode(map<string, string> keysandvalues) {         try {             stringbuilder sb = new stringbuilder();             boolean isfirstentry = true;             (map.entry<string, string> argument : keysandvalues.entryset()) {                 if (isfirstentry) {                     isfirstentry = false;                 } else {                     sb.append("&");                 }                 sb.append(urlencoder.encode(argument.getkey(), "utf-8"));                 sb.append("=");                 sb.append(urlencoder.encode(argument.getvalue(), "utf-8"));             }             return sb.tostring();         } catch (unsupportedencodingexception e) {             //it unlikely system not support utf-8 encoding,              //so not bother polluting method's interface checked exception             throw new runtimeexception(e);          }     } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -