我想获取Internet txt文件的内容。当我用模拟器打开它时,我收到此消息从网址读取文件
“不幸的是,应用程序已停止”。
如果删除“http://”,但它没有读取任何内容并且变成null
,没有任何反应。怎么了?如何读取上面的网址?我已经添加了许可。
这是我更改的整个代码,但仍然有相同的错误。我做了什么错误? http://dforum.zxq.net/code.txt
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL("http://dforum.zxq.net/myfile.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
// TODO Auto-generated method stub
}
}.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
请添加您的logcat输出。 – rekire 2013-04-26 20:59:20
http://dforum.zxq.net/log.txt – 2013-04-26 21:13:33