我想读取远程文本文件并在文本视图中显示其内容。我在下面的代码中编写了这个,但它没有从文本文件中获取任何信息。我怎样才能找到这个问题的原因或解决它?我的代码没有什么问题吗?
private void readFile()
{
try {
String path ="http://host.com/info.txt";
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
Log.e("value",in.toString());
AssetManager mngr=getAssets();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
TextView text = (TextView) findViewById(R.id.TextView1);
text.setText(bo.toString());
bo.close();
}
catch (NetworkOnMainThreadException e) {
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}