I am trying to create a URL but it seems that when I create the URL it isn't created with the full path.
Creating the URL
url = new URL("https://api.plivo.com/v1/Account/" + authID + "/Message/");
When I get the URL path
System.out.println(url.getPath());
The result is: I/System.out: /v1/Account/XXXXXXXXXXXXXXXX/Message/
Does anyone know how can I can solve this?
This is just printing out the "path" part of the URL (after the web address)
Try this to show what is happening, I think there is also a toString in url that will allow you to print the entire thing with just url
URL url = new URL("http://google.com/example");
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(url.getHost() + url.getPath());
This outputs
google.com
/example
google.com/example
Also see the reference
https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#getPath()
SOLVED
Taking just the url returns me the full path
System.out.println(url);
Related
In WebHistoryItem's documentation says:
URL
Return the url of this history item. The url is the base url of this history item. See getTargetUrl() for the url that is the actual target of this history item.
Original URL
Return the original url of this history item. This was the requested url, the final url may be different as there might have been redirects while loading the site.
It makes me confused enough. What I want to know is, what are differences between them? Can you give me some examples?
Thanks in advance.
Well, after I did some research I found differences between them. Original URL is a URL before redirected. Suppose that you opening a shorten link like this https://bit•ly/sG98iK, then you will be redirected to a web page with the following URL https://example.com/android/tutorial/webview.html. We call https://bit•ly/sG98iK as original URL, and https://example.com/android/tutorial/webview.html as URL, i.e. the URL after redirected.
Notice that original URL is nullable.
When you load a website using an URL, the website may redirect to another website with a different URL.
Assume the scenario is like this: Website 1 (URL1) -> Website2 (URL2) -> Website3 (URL3)
then, getUrl will give you URL3. Meanwhile getOriginalURL will return URL1
I get an Address via Geocode which looks like this e.g.: "Downing Street, London" and I build an URL String with this address String and other parameters.
This is how i build the url string:
String url = "http://www.friendlyride.at/...?...&enterstring="+enterstring+"&enterlng="+enterLng+"&enterlat="+enterLat+"&exitstring="+exitstring+"&exitlng="+exitLng+"&exitlat="+exitLat+"&info="+infoinput;
enterstring = "Downing Street, London"
Now when I log the URL (which I then call in a JSON AsyncTask with an HTTPDataHandler), the URL is a link (blue) until an 'ß', space or ',' is in the String.
This is the URL in the Android Monitor (Log.d):
http://www.friendlyride.at/...?...&enterstring=Downing Street, London&enterlng=-0.1272206&enterlat=51.5032077&exitstring=Abbey Rd, London&exitlng=-0.1830032&exitlat=51.5367909&info=info
The whole url should be a link (here it breaks at a space). If I enter the Url manually in the browser, it works with spaces and everything.
So how can i use the whole string as url?
If you need any code, please tell me, I'm not sure what code I should include. :)
Your url needs to be encoded to be valid.
For that purpose and greatly improving your code quality as well, I strongly encourage you using Uri.Builder to build your urls :
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.friendlyride.at")
.appendQueryParameter("enterstring", enterstring)
.appendQueryParameter("enterlng", Long.toString(enterLng))
.appendQueryParameter("enterlat", Long.toString(enterLat));
//Append all your other parameters
String myUrl = builder.build().toString();
This will construct a valid encoded url, like : http://www.friendlyride.at?enterstring=Downing%20Street%2C%20London&enterlng=-0.1272206&enterlat=51.5032077
Note : I do not know what the ...?... part ment in your url, I haven't put it in my answer.
I have an project where I want to load some url came from webservice into a webView but. for that I am facing the following problem. Please help.
url = "www.facebook.com"
wv.loadUrl(url);
for above code I got an error say unable load webpage.
But if i changed this to
url = "https://www.facebook.com"
its working but I need to load url without having http or https mentioned .
Please help me on this.
add this by your own code like
String urlCameFromServer = "www.facebook.com";
if(!urlCameFromServer.contains("http")) {
urlCameFromServer = "http://"+urlCameFromServer;
}
wv.loadUrl(urlCameFromServer);
Has anyone released code to show the full HTTP request/response headers, any intermediate redirects, and any cookie data for the Android HttpURLConnection? This would be similar to Firefox Web Console
I roughly know how to write this myself, but 1) it's a non-trivial amount of code 2) it's tricky to get this kind of code to work in all instances. So i'm interested in finding a readymade solution. I know how to tcpdump the emulator, but I'm searching for code to print this information into the Android Log class for really quick runtime debugging.
for header fields
URL url = new URL(str_url);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.setConnectTimeout(TIMEOUT_SOCKET);
conection.setReadTimeout(TIMEOUT_CONNECTION);
conection.addRequestProperty("Accept-Encoding", "gzip");
RedirectLocations locations = new RedirectLocations();
// here u get all header fields and properties write it in logs
conection.getHeaderFields();
conection.getRequestProperties();
// conection.getOutputStream().write(buffer);
// download the file
InputStream is = conection.getInputStream();
// This is file path were a; quiz data will get saved.
// String file_path = context.getDir(folder,Activity.MODE_PRIVATE).getAbsolutePath();
return unzip(is,save_file_path);
for redirects
link
after u get response, again u ve to look for header fields
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).