Solved

Problem with JSP application when calling Yahoo Geocoding API

Posted on 2008-06-23
13
328 Views
Last Modified: 2008-07-17
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.getInputStream().
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()));

Open in new window

0
Comment
Question by:rnicholus
  • 7
  • 6
13 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 21852245
Does this code work better ?

<%@ page import="java.util.*,java.net.*,java.io.*" %>
<%
  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.setConnectTimeout(120000);
  urlConnection.setReadTimeout(120000);
  urlConnection.setDoInput(true);
  urlConnection.connect();
  BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  String line = null;
  while((line = reader.readLine()) != null)
  out.print(line + "<br/>");
%>
0
 

Author Comment

by:rnicholus
ID: 21857494
I still have the same issue.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21858163
>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.net.*,java.io.*" %>
<%
  response.setContentType("text/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(true);
  urlConnection.connect();
  BufferedReader reader = null;
  if(urlConnection.getResponseCode() ==  HttpURLConnection.HTTP_OK){
                reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  } else {
          reader = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream()));
      }
  String line = null;
  while((line = reader.readLine()) != null)out.print(line);
%>
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:rnicholus
ID: 21860119
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.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21861361
One thing to add to the end of my last posted code.
reader.close();  
Did you add
response.setContentType("text/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.
0
 

Author Comment

by:rnicholus
ID: 21861913
I missed the response.setContentType("text/xml");
Let me give it a try.

Should I close the InputStream also?
0
 
LVL 27

Expert Comment

by:rrz
ID: 21862212
>Should I close the InputStream also?  
No, just add
reader.close();  
to end of my code.  
0
 

Author Comment

by:rnicholus
ID: 21877893
How is the response.setContentType("text/xml"); going to help?
0
 
LVL 27

Expert Comment

by:rrz
ID: 21879546
>How is the response.setContentType("text/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 ?
0
 

Author Comment

by:rnicholus
ID: 21910805
I read the response and if everything looks ok I populate the result (lat long) into a text box.
0
 

Author Comment

by:rnicholus
ID: 21910832
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:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="address"><Latitude>29.780510</Latitude><Longitude>-95.453857</Longitude><Address>I-610 &amp; I-10</Address><City>Houston</City><State>TX</State><Zip></Zip><Country>US</Country></Result></ResultSet>
<!-- ws01.search.re2.yahoo.com compressed/chunked Tue Jul  1 12:33:40 PDT 2008 -->
-------------------------------------------------------------

Very confusing. =(
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 21926057
Your lastest url
>http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street=I-610%20at%20I-10 
doesn't work when used directly in browser. Why don't you use a url like you used above in your original question ?
I don't what exactly yahoo wants in url. Maybe  
http://local.yahooapis.com/MapsService/V1/geocode?appid=APPID&city=Houston&state=TX&street1=I-610&street2=I-10     
But that is just a guess. I didn't read at yahoo.
0
 

Author Comment

by:rnicholus
ID: 22029196
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question