Android java.io.IOException: java.net.URISyntaxException: - java

I'm getting an exception saying Java URI Syntax Exception "java.io.IOException: java.net.URISyntaxException: Invalid % sequence: %wl in query at index 88:" when i try to connect from my android application.
It seems to be throwing the exception where in the URL it says "%wl" and following is the URL. is there a work around for this.
http://192.168.111.111:9000/RB/db.svc/upd?LinkId=184617ED1F21&IPs=fe80::1a46:17ff:feed:1f21%wlan0,192.168.1.127,&MNo=0771111111&sPin=000&Status=0

If you want to use % in your URL the first you need to do is to encode it.
So first you need to replace that % with %25 in your string ....1f21%wlan0... with .....1f21%25wlan0.... before connecting.
You can use the following code for encoding the URL in Java
String encodedUrl = java.net.URLEncoder.encode(<your_url>,"UTF-8");
Have a look at the below links for more information.
1.How to encode url in java
2.URL encoding character reference
UPDATE :
If you don't want to use URL encoder then you can try this out :
yourURL.replaceAll("%", "%25");
It is fine here to replace a single special character, but it would be a tedious task to do like this if you have many special characters that require proper URL encoding.

Related

Twitter RestAPI call from java using space and OR

In this link, it has been explained how to use the Twitter search API, for example:
Your search URL is:
https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
when I do a GET call using the link from POSTMAN (setting OAuth 1 params)it works fine, but when i do the same GET call from JAVA, it gives me the following error:
Caused by: java.net.URISyntaxException: Illegal character in query at index 57: https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
it works fine if i just search with one word without space and OR.
It sounds like you need to encode the URL properly. For instance a space character must be replaced with %20. Here is a table of encodings. Really, you should use a built in function. See this thread.

Need to replace spaces inside string with percentual symbol Java

I need to replace the spaces inside a string with the % symbol but I'm having some issues, what I tried is:
imageUrl = imageUrl.replace(' ', "%20");
But It gives me an error in the replace function.
Then:
imageUrl = imageUrl.replace(' ', "%%20");
But It still gives me an error in the replace function.
The I tried with the unicode symbol:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
But it still gives error.
Is there an easy way to do it?
String.replace(String, String) is the method you want.
replace
imageUrl.replace(' ', "%");
with
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
I suggest you to use a URL Encoder for Encoding Strings in java.
String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");
I've ran into issues like this in the past with certain frameworks. I don't have enough of your code to know for sure, but what might be happening is whatever http framework you are using, in my case it was spring, is encoding the URL again. I spent a few days trying to solve a similar problem where I thought that string replace and the URI.builder() was broken. What ended up being the problem was that my http framework had taken my encoded url, and encoded it again. that means that any place it saw a "%20", it would see the '%' charictor and switch it out for '%' http code, "%25", resulting in. "%2520". The request would then fail because %2520 didn't translate into the space my server was expecting. While the issue apeared to be one of my encoding not working, it was really an issue of encoding too many times. I have an example from some working code in one of my projects below
//the Url of the server
String fullUrl = "http://myapiserver.com/path/";
//The parameter to append. contains a space that will need to be encoded
String param 1 = "parameter 1"
//Use Uri.Builder to append parameter
Uri.Builder uriBuilder = Uri.parse(fullUrl).buildUpon();
uriBuilder.appendQueryParameter("parameter1",param1);
/* Below is where it is important to understand how your
http framework handles unencoded url. In my case, which is Spring
framework, the urls are encoded when performing requests.
The result is that a url that is already encoded will be
encoded twice. For instance, if you're url is
"http://myapiserver.com/path?parameter1=param 1"
and it needs to be read by the server as
"http://myapiserver.com/path?parameter1=param%201"
it makes sense to encode the url using URI.builder().append, or any valid
solutions listed in other posts. However, If the framework is already
encoding your url, then it is likely to run into the issue where you
accidently encode the url twice: Once when you are preparing the URL to be
sent, and once again when you are sending the message through the framework.
this results in sending a url that looks like
"http://myapiserver.com/path?parameter1=param%25201"
where the '%' in "%20" was replaced with "%25", http's representation of '%'
when what you wanted was
"http://myapiserver.com/path?parameter1=param%201"
this can be a difficult bug to squash because you can copy the url in the
debugger prior to it being sent and paste it into a tool like fiddler and
have the fiddler request work but the program request fail.
since my http framework was already encoding the urls, I had to unencode the
urls after appending the parameters so they would only be encoded once.
I'm not saying it's the most gracefull solution, but the code works.
*/
String finalUrl = uriBuilder.build().toString().replace("%2F","/")
.replace("%3A", ":").replace("%20", " ");
//Call the server and ask for the menu. the Menu is saved to a string
//rest.GET() uses spring framework. The url is encoded again as
part of the framework.
menuStringFromIoms = rest.GET(finalUrl);
There is likely a more graceful way to keep a url from encoding twice. I hope this example helps point you on the right direction or eliminate a possability. Good luck.
Try this:
imageUrl = imageUrl.replaceAll(" ", "%20");
Replace spaces is not enought, try this
url = java.net.URLEncoder.encode(url, "UTF-8");

Alchemy api in java gets exception for URLGetRelations()

Alchemy API is used in my program for extracting keywords and relations from a URL.
When extracting these from the API calls I'm getting the error as follows,
'java.io.IOException: Error making API call: cannot-retrieve:downstream-http-error:404.
at com.alchemyapi.api.AlchemyAPI.doRequest(AlchemyAPI.java:960)
at com.alchemyapi.api.AlchemyAPI.GET(AlchemyAPI.java:914)
at com.alchemyapi.api.AlchemyAPI.URLGetRankedKeywords(AlchemyAPI.java:234)
at com.alchemyapi.api.AlchemyAPI.URLGetRankedKeywords(AlchemyAPI.java:224)
at innointel.feature1.Article.alchemyCall(Article.java:477)'
Then I found "http://venturebeat.com/2014/10/22/microsoft-and-ibm-partner-to-bring-enterprise-software-to-their-respective-cloud-platforms/" was the URL causing the error.I called the relation API simply putting the URL as follows ..
Document doc = alchemyObj.URLGetRelations("http://venturebeat.com/2014/10/22/microsoft-and-ibm-partner-to-bring-enterprise-software-to-their-respective-cloud-platforms/");
Well now, it contains no error . What is actually happening here ??
I found in some websites that "cannot-retrieve:downstream-http-error:404" is due to the invalid URL passed as argument.
Out Of 50 URL i tested 7 URL shows the error.Remaining works fine.And again when i extract the URL string placed it as a argument 7 URL works fine too..
(URL is parsed from an excel document using POI API)
Thanks in advance
As you said
'java.io.IOException: Error making API call: cannot-retrieve:downstream-http-error:404'
Exception is caused by the wrong URL argument in function call (ie.URLGetRankedKeywords())
Since the URL is parsed from the EXCEL Document some times the '\r' character might be present at the end of the cell.If present, this will make the URL invalid .
What you can do is , Remove all '\r' character from the URL before you pass it to API call.
ie ,
url = url.replaceAll("\r", "");
Document doc = alchemyObj.URLGetRelations(url);
This might work.it's worked for me.

URL encoding + sign

I have an application with + sign in its name (eg. DB+JSP.jws).
I get an error when trying to create connection as java encodes url + with spaces and hence cannot add the connection to DB JSP/../META-INF/connection.xml (File not found exception).
Any way to circumvent this only by using URLEncoder.encode() and URLDecoder.decode() methods?
You need to encode the URL correctly since '+' is a reserved character in a URL and can only be used in the correct context otherwise needs to be encoded with %2B.
Your URL string would encoded as "DB%2BJSP.jws".
So if you defined the following:
String url = URLEncoder.encode("DB+JSP.jws");
System.out.println(url);
The output would be the same:
DB%2BJSP.jws
You can prepend "http://localhost/" to the encoded URL as you need to.

malformed url exception

https://twitter.com/sessions?authenticity_token=b1b43178e09c1e6ccec1b3183f1f139f39643aaf&session%5Busername_or_email%5D=ddddd&session%5Bpassword%5D=rrrrr&q=&site_action=https%3A%2F%2Ftwitter.com%2Fsessions&serialized_string=Hello
why is this url throwing a java.net.MalformedURLException??
The only reason I can think of is if your code is mistakenly applying the URL decoder to the URL string before trying to parse the URL. That would replace the %xx escapes with the characters they stand for, and you'd end up with illegal characters in the "query" part of the URL.
Please post the code that throws the exception.

Categories