Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build url in java?

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.

like image 917
Manolis Karamanis Avatar asked Oct 29 '14 22:10

Manolis Karamanis


People also ask

How do you create a URL in Java?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

What is URL builder in Java?

A generic toolkit for manipulating and generating URL query strings. UrlBuilder takes a query string in a constructor as the base query to work with. The toString method called immediately after the constructor will return the original input URL. The remove and add parameter methods are evaluated in order.

What is a URI Java?

URI stands for Uniform Resource Identifier. A Uniform Resource Identifier is a sequence of characters used for identification of a particular resource. It enables for the interaction of the representation of the resource over the network using specific protocols.


2 Answers

Try apache's URIBuilder : [Documentation]

import org.apache.http.client.utils.URIBuilder;

// ...

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

Url url = b.build().toUrl();

Maven dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.1</version>
</dependency>
like image 134
jhkuperus Avatar answered Oct 11 '22 00:10

jhkuperus


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.

like image 29
Luiggi Mendoza Avatar answered Oct 11 '22 01:10

Luiggi Mendoza