Search code examples
javaandroidapirssyahoo-api

Get WOEID in Android


I'm developing an app and one of the features I want to include is being able to get the users location and convert it to a WOEID value and then get the weather data from the Yahoo Weather RSS feed. Here's the documentation that states the requirement for the WOEID value: http://developer.yahoo.com/weather/


Solution

  • You can get the WOEID by using the Yahoo! PlaceFinder API. First you need to request an API key, then you can send your request like this:

    http://where.yahooapis.com/geocode?q=[search query]&appid=[yourappidhere]
    

    You will get this as a response:

    <ResultSet xmlns:ns1="http://www.yahooapis.com/v1/base.rng" version="2.0" xml:lang="en-US">
    <Error>0</Error>
    <ErrorMessage>No error</ErrorMessage>
    <Locale>en-US</Locale>
    <Found>1</Found>
    <Quality>85</Quality>
    <Result>
    <quality>85</quality>
    <latitude>38.898708</latitude>
    <longitude>-77.036369</longitude>
    <offsetlat>38.89719</offsetlat>
    <offsetlon>-77.036537</offsetlon>
    <radius>400</radius>
    <name/>
    <line1>1600 Pennsylvania Ave NW</line1>
    <line2>Washington, DC 20500-0005</line2>
    <line3/>
    <line4>United States</line4>
    <house>1600</house>
    <street>Pennsylvania Ave NW</street>
    <xstreet/>
    <unittype/>
    <unit/>
    <postal>20500-0005</postal>
    <neighborhood/>
    <city>Washington</city>
    <county>District of Columbia</county>
    <state>District of Columbia</state>
    <country>United States</country>
    <countrycode>US</countrycode>
    <statecode>DC</statecode>
    <countycode>DC</countycode>
    <uzip>20500</uzip>
    <hash>4216F67AA1A143E9</hash>
    <woeid>12766118</woeid> // This is what you need
    <woetype>11</woetype>
    </Result>
    </ResultSet>
    

    You can parse the response for the <woeid> tag.

    Edit:
    If you have an EditText and you want to get the weather based on the city you get from his input then you'll have to do this:

    String searchQuery = Uri.encode(myEditText.getText().toString());
    

    Now you'll have your search query. So e.g if the user types in:

    Hamburg Germany

    then this will convert it to:

    Hamburg_Germany