インターネットの txt ファイルの内容を取得したいと考えています。エミュレーターで開くと、このメッセージが表示されます
「残念ながらアプリケーションは停止しました」。
「http://」を削除しても何も起こりませんが、何も読み取らず、 になりnull
ます。どうしたの?上記のURLはどうやって読めますか?許可を追加しました。
これは私が変更したコード全体ですが、それでも同じエラーが発生します。私はどんな間違いをしますか? 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;
}
}