I'm using a library that requires me to pass an Uri object as parameter.
Currently I'm doing this:
File file = new File(someFileDir, "test.pdf");
someObject.someFunction(file.toURI());
Note that the File object has the function toURI(). It returns an URI object.
Now my problem is that the function I call needs a Uri parameter, but it's complaining that I passed a URI object.
Aren't these things exactly the same? How can I output an Uri object? Or how do I convert URI to Uri?
Just deal with it as a string, It's easier.
you can use Uri.Builder() to create a Uri !
For you information, URI seem to come from the JAVA language (java.net.URI), and Uri, from the Android SDK specificaly (android.net.Uri). So I would advise you to use the "Uri" class...
Related
LATER EDIT: not same problem as the suggested answer. In my case, I need to build the URI, relying on the fact that the original query string is not modified.
I have a String (coming from a request query String) that is already correctly escaped, like param1=%2Ffolder1%2Ffolder2%2Ffolder%26name%202¶m2=9481dxcv234.
The decoded value of param1 is /folder1/folder2/folder&name 2. Obviously, I cannot unescape that String (because of the & char in this value)...
I need to build an URI which has that original string as query value.
I tried using org.apache.http.client.utils.URIBuilder but could not get it to work: if I provide the original String to the URI(... constructor, the resulting URL is double-escaped, like param1=%252Ffolder1%252Ffolder2%252Ffolder%2526name%25202¶m2=9481dxcv234.
Can I somehow do what I need ? To build an URI by passing the query string already escaped and leave it unchanged ?
Thanks.
I think, the simplest way is unescape it first.
Then you can work with url as usualy.
You could use org.springframework.web.util.UriComponentsBuilder:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
UriComponents components = builder.build(true); //EDIT: pass true when the URI was fully encoded already.
MultiValueMap<String, String> parameters = components.getQueryParams();
parameters.get("param1") //"%2Ffolder1%2Ffolder2%2Ffolder%26name%202"
You can then URLDecode to get what you need.
Edit:
Since you appear to be using Apache HttpClient,
List<NameValuePair> params = org.apache.http.client.utils.URLEncodedUtils.parse(new URI(url), Charset.forName("UTF-8"));
params.get("param1") //"/folder1/folder2/folder&name 2"
I try to generate following uri
//host:port/name/#/name/name
with jersey URI Builder. The Problem is that the uri builder decode the hash character.
Does anyone how to build the given URI?
Thats what I have:
final URI build = uriInfo.getBaseUriBuilder().path("..").path("#").path("/clients/asd/").build();
# is not a valid character in a URI path; # and what follows it is actually a URI fragment.
What you need to do is therefore:
final URI build = uriInfo.getBaseUriBuilder().path("..")
.fragment("/clients/asd/").build();
(note: I don't use Jersey, actually; I looked up the javadoc here, as I suppose this is the same)
For me, this is what worked:
final URI build = UriBuilder.fromUri("{arg1}").fragment("{arg2}").buildFromEncoded("../","/clients/asd/");
Using .path("..").fragment("/clients/asd/").build() would generate
..#%2Fclients%2Fasd
I am confused , how to use Uri because i am using it in android development at Intent's Action dial
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:555-2368"));
What I want to do :
I want to create a file inside which I want to put 2 phone numbers. Then, I want to use toUri() method with this file, and want to put that Uri in the intent, then I want to see what happens with the intent.
According to the Android API docs you can create a file Uri with:
public static Uri fromFile (File file)
java.net.URI is mutable
android.net.Uri is immutable
java.net.URI
A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC 2396.
android.net.Uri
. A URI reference includes a URI and a fragment, the component of the URI following a '#'. Builds and parses URI references which conform to RFC 2396.
In the interest of performance, this class performs little to no validation. Behavior is undefined for invalid input. This class is very forgiving--in the face of invalid input, it will return garbage rather than throw an exception unless otherwise specified.
URIs identify and URLs locate; however, locations are also identifications,
so every URL is also a URI, but there are URIs which are not URLs.
URL - http://example.com/some/page.html
URI - /some/page.html
I want to encode part of URL paramter in JAVA
http://statebuild-dev.com/iit-title/size.xml?id=(102T OR 140T)
to
http://statebuild-dev.com/iit-title/size.xml?id=(102%20OR%20140)
have tried using URI to encode but it also encodes ? which I do not want. In the URL I want to encode the part after '='
URI uri = new URI("http",
"statebuild-dev.com/iit-title", "/size.xml?id=(102 OR 140)", null);
//URL url = uri.toURL();
System.out.println(uri.toString());
System.out.println(url1);
Thank You
you want to use URLEncoder to encode each query parameter before adding to the url, e.g.:
String encodedValue = URLEncoder.encode("(102 OR 140)", "UTF-8");
This answer has a good discussion of encoding the various parts of a URI/URL. You're on the right track, but your specific problem is that you have the various parts of the URI wrong. You need to use the multi-part constructor that takes an authority, path, query, and fragment:
URI uri = new URI("http", "statebuild-dev.com", "/iit-title/size.xml", "id=(102 or 104)", null);
System.out.println(uri.toString());
System.out.println(uri.toASCIIString());
You used the wrong constructor.
Try this:
URI uri = new URI("http","statebuild-dev.com", "/iit-title/size.xml", "id=(102 OR 140)", null);
See also java.net.URLEncoder
The java.net.URI class can help; in the documentation of URL you find
Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI
Check this thread.
I have a question about URI template variables.
I need to manage an URI with the form:
http://netlocation:port/application_path/{variable}
the variable can be a path itself, i.e. something like
this/variable/is/a/path
so that the complete URI appears to be
http://netlocation:port/application_path/this/variable/is/a/path
how can I manage that?
Use the "+" operator in order to avoid escaping the "/" character:
http://netlocation:port/application_path/{+foo}
You can try on URI Template Parser Online
You could use query parameters and just encode the path variable in the standard way:
http://netlocation:port/application_path?path=%2Fthis%2Fvariable%2Fisapath
Have you looked at http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/util/UriTemplate.html ?