0

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

Community
  • 1
  • 1
Neal P
  • 599
  • 1
  • 8
  • 16
  • I can't really post a stack trace because the exception was handled by that try/catch clause. All I have is this: "java.net.SocketException: Permission denied" – Neal P Aug 26 '11 at 01:18
  • nvm, figured it out: http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application @above: "fairfax" was an argument to a format string – Neal P Aug 26 '11 at 01:21
  • 1
    You should at least have posted the message part of the exception. – user207421 Aug 26 '11 at 01:25
  • Have you added INTERNET permission in AndroidManifest file. – Lalit Poptani Aug 26 '11 at 03:09

0 Answers0