Question

The below code works for giving me the text "brendan's android" when I test the code in a regular .java with eclipse but throws me an error exception when I run it in the main activity for the android emulator.

I have tried using HttpURLConnection instead of URLConnection and that did not fix the problem.

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        String urlStr = "http://www.brendan-weinstein.com/android.html";
        URL url = new URL(urlStr);
        URLConnection conn = (URLConnection) url.openConnection();
        InputStreamReader inStream = new InputStreamReader(conn.getInputStream());
        BufferedReader rd = new BufferedReader(inStream);
        String testline = rd.readLine();
        Toast.makeText(GenForm.this, testline, Toast.LENGTH_SHORT).show();
        rd.close();
    }
    catch(IOException ex) {
        Toast.makeText(GenForm.this, "reader did not work", Toast.LENGTH_LONG).show();
    }
}
Was it helpful?

Solution

Could you paste the exception? What kind of exception is it?

Random guess: did you add the need for internet permissions to your manifest? edit:

<uses-permission android:name="android.permission.INTERNET"/> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top