Alchemy api in java gets exception for URLGetRelations() - java

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.

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.

Uri.parse(), how to get the encoding correct?

I am doing an application where I have to read a URL from a webpage as a String[Its not the address of the page]. The URL that I will be reading contains query string, and I specifically need two queries from that URL. So I am using the Uri class available in Android. Now, the problem lies in the encoding/format of the URL and the query. One of the queries that I need is always an URL. Sometimes the query URL is %-encoded and sometimes not.
The URLs can be like the following :
Case 1 :
http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85
Case 2 :
http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85
How do I get the correct Url contained in the file= query?
I am using the following [to accomplish the said work universally] :
Uri.decode(urlString.getQueryParameter("file"));
Is this the correct way to do it?
UPDATE
I have decided to first encode the whole URL regardless of its value and then get the query parameter. Theoretically, it should work.
If you are uncertain about the type of URL you would get then I would suggest you to decode every URL you get from the parameter. And when you need to use it then you can encode it.
As per my knowledge, you are doing it right.

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

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.

java.nio.charset.IllegalCharsetNameException: 'ISO-8859-1'

Jsoup.connect("http://www.design.cmu.edu/community.php?s=3").get();
Could someone please show me why the code gave me the error:
java.nio.charset.IllegalCharsetNameException: 'ISO-8859-1'
The problem is in the target page. It is not well-formed at all.
When parsing the page, JSoup tries to fix the page and for one thing, parses the content type to "text/html; charset='iso-8859-1'"(with the single quotes included).
It then passes this string(with the single quotes) and uses it to get the charset:
Charset.forName("'ISO-8859-1'");
which fails.
The problem is in the target page.
Maybe you can use this alternative instead, which doesn't parse the charset from the page, because you explicitly pass it along:
String url = "http://www.design.cmu.edu/community.php?s=3";
Document document = Jsoup.parse(new URL(url).openStream(), "ISO-8859-1", url);

Returned URL as String is not valid in JSF

I'm trying to make use of google api as text-to-speech. So, I build a String then should pass it as a URL to a component to obtain a MP3 with the spoken words.
So, this is my code:
URI uri = new URI("http://translate.google.com/translate_tts?tl=es&q="+ URLEncoder.encode((String)this.text.getValue(), "UTF-8"));
When I make uri.toString() its return a well formed URL. If I copy and paste this output in the browser works pefectly.
But if I assign this returned String to the source property of a ice:outputMedia is not working. Then inspect the HTML generated in the page and the String in src property is:
http://translate.google.com/translate_tts?tl=es&q=Bobby+need+peanuts
The & symbol has been replaced by &.
How can I avoid this to make a valid URL?
You need to decode the url on the client side using Javascript.
var decoded = decodeURI(URI)

Categories