以下のコードは、通常の.javaでeclipseを使用してコードをテストすると、「brendan's android」というテキストを表示するために機能しますが、androidエミュレーターのメインアクティビティで実行するとエラー例外がスローされます。
URLConnectionの代わりにHttpURLConnectionを使用してみましたが、問題は解決しませんでした。
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();
}
}