我无法使用URL类从Android工作区中的文件中读取文本。我得到一个NetworkOnMainThreadException。相同的确切代码在Android工作区以外工作,因此它必须是网络或权限问题。我从指定的链接生成了一个基本的文本文件。 它似乎在url.openStream上失败。 下面是我的代码,然后我的清单文件(其中我已经添加了应用的权限行。)Android无法从URL读取
static public String[] barList()
{
ArrayList<String> bars = new ArrayList<String>();
try{
URL url = new URL("****");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = br.readLine();
while(line != null)
{
bars.add(line);
line = br.readLine();
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
return (String[])bars.toArray();
}
这里是清单文件。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aexp.explist">
<application>
<activity android:name=".ExpList" android:label="ExpList">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
是例外发生了什么暗示? 'e.getMessage()' – Mike
由于某种原因,当我调试程序时,这个异常并没有引起注意。而不是把我带到catch块,它带我到一个“Class.class”文件。 – Glockstock
发生这种情况并且您处于DDMS视角时,请单击绿色的“播放”按钮继续。如有必要,请多做几次,最终您应该在LogCat选项卡中看到堆栈跟踪。顺便说一下,我仅在Internet模拟器中测试了您的代码,但没有发现任何错误。 – Mike