gpt4 book ai didi

java - 为什么我用来从网站读取文件的代码可以在 java 中运行,但不能在 android 模拟器中运行?

转载 作者:行者123 更新时间:2023-12-02 08:26:14 24 4
gpt4 key购买 nike

当我使用 Eclipse 在常规 .java 中测试代码时,下面的代码可以为我提供文本“brendan's android”,但当我在 Android 模拟器的主 Activity 中运行它时,会引发错误异常。

我尝试使用 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();
}
}

最佳答案

你能粘贴异常吗?这是什么异常?

随机猜测:您是否将互联网权限的需求添加到 list 中?编辑:

<uses-permission android:name="android.permission.INTERNET"/> 

关于java - 为什么我用来从网站读取文件的代码可以在 java 中运行,但不能在 android 模拟器中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4477537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com