drewsmug drewsmug - 2 months ago 23
Java Question

bufferReader or InputStreamReader always throws exception

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");
}

Answer

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.

Comments