인터넷 txt 파일의 콘텐츠를 가져오고 싶습니다. 에뮬레이터로 열면이 메시지가 나타납니다.URL에서 파일 읽기
"불행히도 응용 프로그램이 중지되었습니다."
"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;
}
}
으로 바꾸십시오. logcat 출력을 추가하십시오. – rekire
http://dforum.zxq.net/log.txt –