Given the following Android Java code:
import android.net.Uri;
String humanEnteredString = "google.com";
Uri uri = Uri.parse(humanEnteredString);
Why is uri.getPath() == "google.com", and not uri.getAuthority()?
How to force Uri.parse to see "google.com" as the authority?
Is that what classes like this are for?
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/WebAddress.java
Why is this WebAddress class not accessible in the Android SDK?
Is there any pre-built class that can generate a valid <scheme>://<authority><path> browsable Uri from a human entered string that may be missing the scheme?
Obviously, I can test for and prefix "http[s]://" as necessary/appropriate, but shouldn't there already be a bulletproof class that does this already?
Thanks,
Pv
Why is uri.getPath() == "google.com", and not uri.getAuthority()?
Because "google.com" is not a valid Uri.
How to force Uri.parse to see "google.com" as the authority?
Use a valid Uri: one with a scheme, such as https://google.com.
Is there any pre-built class that can generate a valid :// browsable Uri from a human entered string that may be missing the scheme?
Not in the Android SDK, at least that I can recall.
Related
I was wondering if I can hardcore the file:// prefix into one of my functions in android.
The function is supposed to determine whether or not the given link points to an external resource on the web, or an internal resource inside the phone itself
public Uri generate_image_uri(String link)
{
// link can be "1DCHiI2.jpg"
// link can be "file://smiley_face.jpg"
if (!link.startsWith("file://")
return Uri.parse("https://i.imgur.com/" + link);
else
return Uri.parse(link);
}
Is this advisable? Or is there a more "fault tolerant" way of getting file://? maybe some function like getProperFilePrefixForThisAndroidVersion();?
In order to clarify my question:
given the following code
(new File(getFilesDir(), "hello_world.jpg")).toString();
Is it safe to assume within reasonable probability that the resulting string will always start with file:// in all current and future Android versions?
Given:
(new File(getFilesDir(), "hello_world.jpg")).toString();
is it safe to assume within reasonable probability that the resulting string will always start with file:// in all current and future Android versions?
No.
According to the javadoc for File on Android, File.toString returns:
"... the pathname string of this abstract pathname. This is just the string returned by the getPath() method."
Not a "file://" URL.
If you want to get a properly formed "file://" URL, do this:
new File(...).toURI().toString()
Now, technically the protocol for a URL (i.e. "file") is case insensitive:
Is the protocol name in URLs case sensitive?
Which means that "FILE://" or "File://" etc are technically valid alternatives.
However, the probability that above expression would ever emit anything other than the lower-case form of the protocol is (um) vanishingly small1.
1 - It would entail monumentally stupid decision making by a number of people. And they are NOT stupid people.
Well there are lot of discussion, post, comments and questions over internet to differentiate URI, URL and URN.
One answer on SO explain about it, but i am confused in implementation result in my code.
Q : If URI is super set of URL then how come it got this following output:
URI : /XXX/abc.do
URL : http://examplehost:8080/XXX/abc.do
When i write the below code:
System.out.println(“URI : “+ httpRequestObj.getRequestURI());
System.out.println(“URL : “+ httpRequestObj.getRequestURL());
EDIT : Could you share a detailed answer by keeping JAVA and original concept of URI,URL and URN in scope.
Regards,
Arun Kumar
java.net.URI API gives a good explanation:
A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.
If URI is super set of URL then how come it got this following output ...
The definitions of URI and URL cannot be used to infer the behaviour of getRequestURI() and getRequestURL(). To understand what the methods return, you need to read the javadocs and the Servlet specification.
The meaning of those methods are what they are because the HttpRequest API has evolved over time, and that evolution has had to maintain backwards compatibility.
getRequestURI() does return a URI, and getRequestURL() does return a URL, but the URI and URL are for different things.
The project i am working on have multiple instances(i.e different website) running in single code base. Based on the URL we show corresponding website.
For example, if http://www.uswebsite.com/ the we show US website. and ifhttp://www.cawebsite.com/ will show Canadian website. The code that is written to detect this is
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String server` = httpRequest.getServerName();
If request is from the http://www.uswebsite.com/ then according to above code String server = uswebsite so we have additional code written to pop up the corresponding site
we are now planning to include the European instance in the same code base and i see that the URL for the europe site is going to be like this http://www.europewebsite.co.uk/. With getServer() as above will it fetch String server=europewebsite. By having the .co.uk at the end will the above code still get String server=europewebsite or String server=europewebsite.co Please advise as i can't test it out this in my local.
The wording of your question is rather confusing, so I'm assuming that you simply want to extract the hostname from a hierarchical URL string.
The most reliable way to do it is to construct an instance of java.net.URL or java.net.URI for the url string, and use the relevant getter to extract the hostname.
On the other hand, if you are trying to detect the virtual hostname for the current request in a servlet, then your httpRequest.getServerName() gives you that hostname as it appears in the first line of the HTTP request (unless something has rewritten it ...).
On the other hand, if you simply want to extract "europewebsite" from a DNS name like "www.europewebsite.co.uk", there is no general solution. I'd recommend converting the DNS to all lowercase and doing a lookup in a table that you have pre-initialized with all of the variations that you are prepared to recognize.
Normally httpRequest.getServerName() will return hostname, in your case it is complete name ie. "europewebsite.co.uk".
I've got a string:
public://imageifarm/3600.jpg
How can I extract the
imageifarm/3600.jpg
Part out using android?
What I've tried so far:
URL drupalQuestionNodeImageURI = new URL("public://imageifarm/3600.jpg");
Log.d("TAG", drupalQuestionNodeImageURI.getPath());
but it throws this exception:
09-16 17:24:39.992: W/System.err(3763): java.net.MalformedURLException: Unknown protocol: public
How can I solve this?
I know I can use regular expressions but that seems to defeat the purpose of URL(URI) in this case.
You should use android.net.Uri
Uri mUri = Uri.parse(public://imageifarm/3600.jpg);
String extract = mUri.getEncodedSchemeSpecificPart();
Use java.net.URI, not java.net.URL.
If you want have to use URL class (when you image sits on Internet) you have to provide valid URL (that begins from valid URL prefix, like http://, https:// etc). In you case you should use Uri class. Uri object can point on files in your local file system. For example:
Uri.fromFile(new File("public://imageifarm/3600.jpg"));
Simple question: why am I getting new IllegalArgumentException: Path component should be '/' when trying to create a zip filesystem at the following URI:
file:E:/somedirectory/somefile
But this seems to work: file:/somedirectory/somefile
What if I have the same paths on two different drives and I need to access a specific one? Or am I completely missing the point of URIs in the first place?
For paths that use windows volumes use the following format:
file:///e:/somedirectory/somefile
The triple /// results from omitting the URL hostname for local files. Compare: file://sometherhost/e:/somedirectory/somefile, which is valid according to the URI spec, if not actually useful for accessing files on remote volumes.
1. Backslashes are used to point directories and files
2. Try it this way...
`E:\\somedirectory\\somefile`
Maybe its easier to do it with the URI builder. I always use it:
URIBuilder builder = new URIBuilder();
builder.setSchema("file").setHost("anyhost").setPath("/yourpath/");
URI uri;
uri = builder.build();
you can check your URI:
System.out.println(uri.toString());
I hope this will help you!