Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am building a String with StringBuilder

StringBuilder builder = new StringBuilder();
builder.append("my parameters");
builder.append("other parameters");

Then i build a Url

Url url = new Url(builder.toString());

And then i try the connection

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

But the url seems not to be right from the results i get. It's like some parameter is being false passed. That's why i think the problem is in the part of the StringBuilder.

The problem is in a double parameter i try to pass.

double longitude = 23.433114;
String lng = String.ValueOf(longitude);

And then i put it in the url. But if i give it as a string the result is correct.

String lng = "23.433114"

Is UrlEncoding necessary? I will try what is suggested below.

share|improve this question
    
Are you URLencoding each parameter value? – RealSkeptic Oct 29 '14 at 22:35
1  
@RealSkeptic URLEncoder.encode() encodes for application/x-www-form-urlencoded`, which is quite different from encoding query parameters/URI fragments/etc – fge Oct 29 '14 at 22:38
1  
@fge No, URLencode is a standard encoding scheme. It is used both for parameters in GET queries and for parameters in POST queries which are application/x-www-form-urlencoded. See Wikipedia. – RealSkeptic Oct 29 '14 at 22:46
1  
@RealSkeptic @EJP sorry but that's wrong. In query parameters, for instance, a space becomes %20; as I said, the method you mention encodes for forms in which the space becomes +. And that is only one example – fge Oct 29 '14 at 22:48
1  
@fge - try both of them in a query. You'll be surprised. + Is an acceptable replacement for space in GET queries. But if you don't believe Wikipedia, Try The HTML Spec – RealSkeptic Oct 29 '14 at 22:54
up vote 29 down vote accepted

Try apache's URIBuilder : [Documentation]

URIBuilder b = new URIBuilder("http://example.com");
b.addParameter("t", "search");
b.addParameter("q", "apples");

Url url = b.build().toUrl();
share|improve this answer
1  
Works flawlessly. The only problem is that it throws checked exceptions which are boring to handle. – Áron Lőrincz Jun 19 '15 at 8:47

Since you want to create the URL and consume it through a GET request, it would be better to use a library that helps you in this process. You can use HttpComponents or another library like Unirest that is built on top of HttpComponents which ease all this work.

Here's an example using Unirest:

HttpResponse<String> stringResponse = Unirest.get("https://www.youtube.com/results")
    .field("search_query", "eñe")
    .asString();
System.out.println(stringResponse.getBody());

This will retrieve the HTML response corresponding to all the results from a search on youtube using "eñe". The ñ character will be encoded for you.

DISCLAIMER: I'm not attached to Unirest in any mean. I'm not a developer or a sponsor of this project. I'm only a happy user of this framework.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.