Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Google Webservice Problem

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Google Webservice Problem

    Hi, I am new to the forums here and was hoping someone could help me out. I have been working on this project and have been stuck for quite a while now(almost a week). I am learning about how to connect java to the web and would like to do the example of connecting to a google webservice. (specifically the geocoding webservice) I have created a valid URL I know because it works in the browser, but I just get a thrown exception (http 400---bad request) when I try to call upon the URL from my java code.

    Thanks in advance for any help. I'm truly stumped!

    Jim
    private void searchGoogle(String street, String city, String state)
        {
            URL url = null;
            URLConnection connection = null;
            InputSource inputxml = null;
            NodeList nodes = null;
            Permission per= null;
     
            //run a search on google server and display results in list
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
     
     
     
            //replace all spaces in search strings with "+"
            street.replace(' ', '+');
            city.replace(' ', '+');
            state.replace(' ', '+');
     
            String request = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
            request = request.concat(street);
            request = request.concat(",+");
            request = request.concat(city);
            request = request.concat(",+");
            request = request.concat(state);
            request = request.concat("&sensor=false");
     
            try
            {
     
                url = new URL(request);
     
                connection = url.openConnection();
     
                inputxml = new InputSource(connection.getInputStream());
                nodes = (NodeList) xpath.evaluate("XPATH_EXPRESSION", inputxml, XPathConstants.NODESET);
            }
            catch(Exception MalformedURLException){
                System.out.print("connection error");
            }
     
     
     
     
            // We can then iterate over the NodeList and extract the content via getTextContent().
          // NOTE: this will only return text for element nodes at the returned context.
          for (int i = 0, n = nodes.getLength(); i < n; i++) {
            String nodeString = nodes.item(i).getTextContent();
            System.out.print(nodeString);
            System.out.print("\n");
            }
     
        }
    Last edited by copeg; November 14th, 2010 at 08:35 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Google Webservice Problem

    First, be sure you are not violating the google terms of use. Second, set the User-Agent request property for the URLConnection. I cannot recall the exact code, but something like:
    connection.setRequestProperty("User-Agent", "Mozilla 6.0");
    Again, not sure if the above will work but you should be able to find several examples with a google search on how to set the user agent. Lastly, be sure you are not violating the google terms of use.

Similar Threads

  1. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: February 26th, 2012, 04:21 PM
  2. google search
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: April 2nd, 2010, 03:13 AM
  3. Exception consuming WebService
    By lzorratto in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: February 23rd, 2010, 09:13 AM
  4. Java Webservice problem (Array)
    By Gadge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2010, 11:06 AM

Tags for this Thread