从 Android 上的服务器读取文本文件 [英] Reading Text File From Server on Android
本文介绍了从 Android 上的服务器读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的服务器上有一个文本文件.我想从我的 Android 应用程序打开文本文件,然后在 TextView 中显示文本.我找不到任何关于如何与服务器建立基本连接并将数据输入字符串的示例.
如果您能提供任何帮助,我们将不胜感激.
解决方案
尝试以下操作:
尝试{//创建所需页面的 URLURL url = new URL("mysite.com/thefile.txt");//读取服务器返回的所有文本BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));字符串 str;while ((str = in.readLine()) != null) {//str 是一行文本;readLine() 去除换行符}附寄();} catch (MalformedURLException e) {} catch (IOException e) {}
(取自 Exampledepot:从 URL 获取文本)>
应该在 Android 上运行良好.
I have a text file on my server. I want to open the text file from my Android App and then display the text in a TextView. I cannot find any examples of how to do a basic connection to a server and feed the data into a String.
Any help you can provide would be appreciated.
解决方案
Try the following:
try {
// Create a URL for the desired page
URL url = new URL("mysite.com/thefile.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
(taken from Exampledepot: Getting text from URL)
Should work well on Android.
这篇关于从 Android 上的服务器读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文