1

I created a java program that would just read a webpages code... google's for example and it works fine. However I have tried to implement the same code in an android application both in the emulator and on the actual device-Droid X.

I've tried two separate ways and have found either way it is the same line that throws the IOException. It is the line that creates the new bufferReader. I don't know if it is the bufferReader or the InputStreamReader. Also I don't really know how to get anymore information out of the exceptions other than to print out IOException. Here is the important code stripped down. Thanks.

try
        {
//method 1
         URL page = new URL("http://192.168.1.108/score.php");
         URLConnection pageconnection = page.openConnection();
         BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    pageconnection.getInputStream()));

//method 2
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            BufferedReader rd  = new BufferedReader(new InputStreamReader(conn.getInputStream()));

while ((inputLine = rd.readLine()) != null) 
            System.out.println(inputLine);

          in.close();
          rd.close();
            HscoreText.setText("It Worked!");
        }
        catch(MalformedURLException e)
        {
         HscoreText.setText("MalformedURL");
        }
        catch (IOException e)
        {
         HscoreText.setText("IOException");
        }
2
  • 1
    Please post the full stacktrace of your exception. Use Log.d("message", e); Jan 18, 2011 at 21:53
  • Call e.toString() to convert the exception into a string... that will give considerably more information, with any luck.
    – Jon Skeet
    Jan 18, 2011 at 21:54

4 Answers 4

1

Instead of doing this,

HscoreText.setText("IOException");

You can do this.

HscoreText.setText(e.getMessage());

to see the exception message. Are you saying that your code works outside of Android?

Try restarting Eclipse and the Emulator. Try using a different URL.

2
  • Thanks. I have more information now. My error message says "java.net.SocketException: Permission denied (maybe missing INTERNET permission)". Any clue what that might mean?
    – drewsmug
    Jan 18, 2011 at 23:15
  • 1
    I added <uses-permission android:name="android.permission.INTERNET" /> to my manifest and it worked. Thanks
    – drewsmug
    Jan 18, 2011 at 23:27
0

It's worth noting that your code (both versions) assumes that the HTML returned has the same character encoding as your platform default encoding. This may not be true even if the server is running on the same machine.

0

Or shameless plug you can use Resty and don't bother with URLConnection and stuff.

Resty r = new Resty();
String text = r.text("http://192.168.1.108/score.php").toString();

It will honor the content encoding and gets out of your way otherwise.

-1

Try this - setDoInput (true) and setDoOutput(true) after conn.connect() ;

1
  • 1
    Try it why? setDoInput(true) is the default, so that's a waste of time, and setDoOutput(true) sets the request method to POST, which isn't mentioned in the question, nor any parameters thant need to be posted, so that's a waste of time as well.
    – user207421
    Jul 5, 2016 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.