当我在eclipse中使用常规.java测试代码时,下面的代码可以为我提供文本“brendan的android”,但是当我在android模拟器的主活动中运行它时会抛出一个错误异常。
我尝试过使用HttpURLConnection而不是URLConnection,并没有解决问题。
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();
}
}
答案 0 :(得分:2)
你可以粘贴例外吗?它有什么例外?
随机猜测:您是否在清单中添加了对互联网权限的需求? 编辑:
<uses-permission android:name="android.permission.INTERNET"/>