Possible Duplicate:
What permission do I need to access Internet from an android application?
I came across the Google Weather API in this post: http://blog.programmableweb.com/2010/02/08/googles-secret-weather-api/.
I wanted to try it out, and it worked fine on Windows. When I tried it on my Android 2.2 phone and the Android emulator, the code below threw a SocketException at the point commented below.
Here is the code:
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;
}
It was basically called like this:
getWeather("fairfax");
It works fine on a pc. http://www.google.com/ig/api?weather=fairfax