この投稿で Google Weather API に出会いました: http://blog.programmableweb.com/2010/02/08/googles-secret-weather-api/。
試してみたかったのですが、Windowsで問題なく動作しました。Android 2.2 携帯電話と Android エミュレーターで試してみたところ、以下のコードは、以下にコメントした時点で SocketException をスローしました。
コードは次のとおりです。
public static String downloadPage(String pageUrl) throws IOException
{
URL url = new URL(pageUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if (conn == null)
return null;
conn.setRequestMethod("GET");
conn.setDoOutput(true);
//this throws a SocketException
conn.connect();
InputStream is = conn.getInputStream();
//InputStream is = url.openStream();
if (is == null)
return null;
InputStreamReader rdr = new InputStreamReader(is);
StringBuilder sb = new StringBuilder();
int b;
while ((b = rdr.read()) != -1)
{
sb.append((char)b);
}
return sb.toString();
}
public static boolean getWeather(String city, ArrayList<String> stats)
{
String url = String.format("http://www.google.com/ig/api?weather=%s",
city.replace(' ', '+'));
try
{
String XML = downloadPage(url);
//...
return true;
}
catch (Exception exc)
{
exc.getCause();
}
return false;
}
基本的には次のように呼び出されました。
getWeather("fairfax");
PC上では問題なく動作します。http://www.google.com/ig/api?weather=fairfax