Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URIBuilder setParameter adds + instead of %20 for space

I'm trying to build a URL using URIBuilder and for some reason I get + instead of %20 for space.

this is my code:

            URI uri=null;
            try {
                uri = ub.setScheme("http")
                        .setHost("myalcoholist.com")
                        .setPath("/drink-management/view-drink-json")
                        .setParameter("flavor",(String)params[1])
                        .setParameter("brand",(String)params[2])
                        .setParameter("company",(String)params[3])
                        .build();
            } catch (URISyntaxException e) {
                Log.e("url", "could not parse url", e);
                return null;
            }

the parameters:

params[3] = {java.lang.String@831933398520}"contreau france."
params[2] = {java.lang.String@831933398384}"cointreau"
params[1] = {java.lang.String@831933398256}"orange"

the result URL:

"GET /drink-management/view-drink-json?flavor=orange&brand=cointreau&company=contreau+france. HTTP/1.1" 200 538 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

what do I need to change for the parameters to be properly encoded ?

thanks

like image 966
ufk Avatar asked Jun 06 '14 14:06

ufk


1 Answers

I know this post is very old, but since it's the first result google gave me and had no answers, here is the way I resolved the problem:

Just use URLEncoder.encode(string, "UTF-8") with the String (string being your own string) you want to make this change. This will encode all the characters in UTF-8 in your String to URL format.

like image 58
Javier Posse Avatar answered Sep 21 '22 04:09

Javier Posse