0

Simple Java implementation

The following code might be useful for somebody who wants to access the API via Java

CODEBOX
package com.example.yahoogeo;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class yahooGeoClient {
private static final String yahooApplicationId = <your AppId goes here as String>;

/**
* @param args
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws IOException {
Document xmlResponse = makeYahooGeoSearch();
// do something here with the DOM document
}

private static Document makeYahooGeoSearch() {

String searchUrl = "http://wherein.yahooapis.com/v1/document";
String documentContent = "Monaco";
String documentTitle = "";
String documentType = "text/plain";
String outputType = "xml";
String inputLanguage = "en-US";
String focusWoeid = "";
String autoDisambiguate = "false";

Document xmlDocument = null;
try {
URL url = new URL(searchUrl);
String data = "documentContent=" + URLEncoder.encode(documentContent,"UTF-8")
+ "&" + "documentTitle=" + URLEncoder.encode(documentTitle,"UTF-8")
+ "&" + "appid=" + URLEncoder.encode(yahooApplicationId, "UTF-8")
+ "&" + "documentType=" + URLEncoder.encode(documentType,"UTF-8")
+ "&" + "outputType=" + URLEncoder.encode(outputType, "UTF-8")
+ "&" + "inputLanguage=" + URLEncoder.encode(inputLanguage, "UTF-8")
+ "&" + "focusWoeid=" + URLEncoder.encode(focusWoeid, "UTF-8")
+ "&" + "autoDisambiguate=" + URLEncoder.encode(autoDisambiguate, "UTF-8")
;

// set up the connection
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches (false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// send request (with data)
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(data);
writer.flush();
writer.close();

// get the response
xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getInputStream());

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return xmlDocument;
}
}

by
2 Replies


  • QUOTE(Matthias @ 4 May 2010 2:12 AM)
    The following code might be useful for somebody who wants to access the API via Java

    CODEBOX
    package com.example.yahoogeo;

    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;

    public class yahooGeoClient {
    private static final String yahooApplicationId = ;

    /**
    * @param args
    * @throws IOException
    * @throws ParseException
    */
    public static void main(String[] args) throws IOException {
    Document xmlResponse = makeYahooGeoSearch();
    // do something here with the DOM document
    }

    private static Document makeYahooGeoSearch() {

    String searchUrl = "http://wherein.yahooapis.com/v1/document";
    String documentContent = "Monaco";
    String documentTitle = "";
    String documentType = "text/plain";
    String outputType = "xml";
    String inputLanguage = "en-US";
    String focusWoeid = "";
    String autoDisambiguate = "false";

    Document xmlDocument = null;
    try {
    URL url = new URL(searchUrl);
    String data = "documentContent=" + URLEncoder.encode(documentContent,"UTF-8")
    + "&" + "documentTitle=" + URLEncoder.encode(documentTitle,"UTF-8")
    + "&" + "appid=" + URLEncoder.encode(yahooApplicationId, "UTF-8")
    + "&" + "documentType=" + URLEncoder.encode(documentType,"UTF-8")
    + "&" + "outputType=" + URLEncoder.encode(outputType, "UTF-8")
    + "&" + "inputLanguage=" + URLEncoder.encode(inputLanguage, "UTF-8")
    + "&" + "focusWoeid=" + URLEncoder.encode(focusWoeid, "UTF-8")
    + "&" + "autoDisambiguate=" + URLEncoder.encode(autoDisambiguate, "UTF-8")
    ;

    // set up the connection
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches (false);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    // send request (with data)
    DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
    writer.writeBytes(data);
    writer.flush();
    writer.close();

    // get the response
    xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getInputStream());

    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return xmlDocument;
    }
    }
    1


  • QUOTE( @ )
    0
This forum is locked.

Recent Posts

in Placemaker Applications and Demos