rnicholus
asked on
Problem with JSP application when calling Yahoo Geocoding API
Hello,
I'm trying to use YAHOO geocoding API in my JSP page.
Basically I have a form with a text box to fill in 'address' and a button that will call YAHOO geocoding API. But many times I got HTTP response 400 but when I try to submit the request again (same 'address' values) it is ok. I'm trying to figure out whether this is related to the JSP part or not.I don't understand why sometime it works and sometime it doesn't. Maybe some of you guys might have similar experiences?
I think it randomly gives me bad request/ HTTP 400 when I do urlConnection.getInputStre am().
I have never had this problem when I call this geocoding URL directly from the web browser.
Thanks in advance for all the help.
I'm trying to use YAHOO geocoding API in my JSP page.
Basically I have a form with a text box to fill in 'address' and a button that will call YAHOO geocoding API. But many times I got HTTP response 400 but when I try to submit the request again (same 'address' values) it is ok. I'm trying to figure out whether this is related to the JSP part or not.I don't understand why sometime it works and sometime it doesn't. Maybe some of you guys might have similar experiences?
I think it randomly gives me bad request/ HTTP 400 when I do urlConnection.getInputStre
I have never had this problem when I call this geocoding URL directly from the web browser.
Thanks in advance for all the help.
String url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=APP_ID&city=Houston&state=TX&street=I-610&I-45"
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(120000);
urlConnection.setReadTimeout(120000);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
ASKER
I still have the same issue.
>I have never had this problem when I call this geocoding URL directly from the web browser.
Ah that is the clue we needed. Try this page.
<%@ page import="java.util.*,java.n et.*,java. io.*" %>
<%
response.setContentType("t ext/xml");
String url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=APP_ID&city=Houston&state=TX&street=I-610&I-45";
HttpURLConnection urlConnection = (HttpURLConnection)(new URL(url).openConnection()) ;
urlConnection.setDoInput(t rue);
urlConnection.connect();
BufferedReader reader = null;
if(urlConnection.getRespon seCode() == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(urlConne ction.getI nputStream ()));
} else {
reader = new BufferedReader(new InputStreamReader(urlConne ction.getE rrorStream ()));
}
String line = null;
while((line = reader.readLine()) != null)out.print(line);
%>
Ah that is the clue we needed. Try this page.
<%@ page import="java.util.*,java.n
<%
response.setContentType("t
String url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=APP_ID&city=Houston&state=TX&street=I-610&I-45";
HttpURLConnection urlConnection = (HttpURLConnection)(new URL(url).openConnection())
urlConnection.setDoInput(t
urlConnection.connect();
BufferedReader reader = null;
if(urlConnection.getRespon
reader = new BufferedReader(new InputStreamReader(urlConne
} else {
reader = new BufferedReader(new InputStreamReader(urlConne
}
String line = null;
while((line = reader.readLine()) != null)out.print(line);
%>
ASKER
I followed your example:
I tried to put "I-610%20at%20I-45" (read: I-610 at I-45) in the street parameter and it gave me this on the first try.
-------------------------- ---------- ------
The following errors were detected:
unable to parse location
-------------------------- ---------- ------
But successful at the second try.
But other times it is okay on the first try.
I tried to put "I-610%20at%20I-45" (read: I-610 at I-45) in the street parameter and it gave me this on the first try.
--------------------------
The following errors were detected:
unable to parse location
--------------------------
But successful at the second try.
But other times it is okay on the first try.
One thing to add to the end of my last posted code.
reader.close();
Did you add
response.setContentType("t ext/xml");
to your code(like I did above) ?
That is all I got. It works for me every time. Maybe an expert will see something else.
reader.close();
Did you add
response.setContentType("t
to your code(like I did above) ?
That is all I got. It works for me every time. Maybe an expert will see something else.
ASKER
I missed the response.setContentType("t ext/xml");
Let me give it a try.
Should I close the InputStream also?
Let me give it a try.
Should I close the InputStream also?
>Should I close the InputStream also?
No, just add
reader.close();
to end of my code.
No, just add
reader.close();
to end of my code.
ASKER
How is the response.setContentType("t ext/xml"); going to help?
>How is the response.setContentType("t ext/xml"); going to help?
It helped my testing. When the response was received in my browser(IE 7) it knew how to display the content.
What are you doing with the response ?
It helped my testing. When the response was received in my browser(IE 7) it knew how to display the content.
What are you doing with the response ?
ASKER
I read the response and if everything looks ok I populate the result (lat long) into a text box.
ASKER
I think I saw the same error now in web browser (Mozilla):
http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street=I-610%20at%20I-10
Here's what I saw in the broswer:
-------------------------- ---------- ---------- ---------- -----
<Error>
The following errors were detected:
<Message>unable to parse location</Message>
</Error>
<!--
ws01.search.re2.yahoo.com uncompressed/chunked Tue Jul 1 12:32:22 PDT 2008
-------------------------- ---------- ---------- ---------- -----
but when I do view source, the result is there:
-------------------------- ---------- ---------- ---------- -----
<?xml version="1.0"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:ya hoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="address"><Latit ude>29.780 510</Latit ude><Longi tude>-95.4 53857</Lon gitude><Ad dress>I-61 0 & I-10</Address><City>Housto n</City><S tate>TX</S tate><Zip> </Zip><Cou ntry>US</C ountry></R esult></Re sultSet>
<!-- ws01.search.re2.yahoo.com compressed/chunked Tue Jul 1 12:33:40 PDT 2008 -->
-------------------------- ---------- ---------- ---------- -----
Very confusing. =(
http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street=I-610%20at%20I-10
Here's what I saw in the broswer:
--------------------------
<Error>
The following errors were detected:
<Message>unable to parse location</Message>
</Error>
<!--
ws01.search.re2.yahoo.com uncompressed/chunked Tue Jul 1 12:32:22 PDT 2008
--------------------------
but when I do view source, the result is there:
--------------------------
<?xml version="1.0"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:ya
<!-- ws01.search.re2.yahoo.com compressed/chunked Tue Jul 1 12:33:40 PDT 2008 -->
--------------------------
Very confusing. =(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now, I see the problem:
For : http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street=I-610%20at%20I-10
Sometime it works, sometime it doesn't. I'm not sure why. I think it's something not right on the yahoo side. I will give you points for your assistance. Thank you.
For : http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street=I-610%20at%20I-10
Sometime it works, sometime it doesn't. I'm not sure why. I think it's something not right on the yahoo side. I will give you points for your assistance. Thank you.
<%@ page import="java.util.*,java.n
<%
String url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=APP_ID&city=Houston&state=TX&street=I-610&I-45";
HttpURLConnection urlConnection = (HttpURLConnection)(new URL(url).openConnection())
urlConnection.setConnectTi
urlConnection.setReadTimeo
urlConnection.setDoInput(t
urlConnection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConne
String line = null;
while((line = reader.readLine()) != null)
out.print(line + "<br/>");
%>