从 Android 上的服务器读取文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2922210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading Text File From Server on Android
提问by Chris
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.
我的服务器上有一个文本文件。我想从我的 Android 应用程序打开文本文件,然后在 TextView 中显示文本。我找不到任何关于如何与服务器建立基本连接并将数据输入字符串的示例。
Any help you can provide would be appreciated.
您能提供的任何帮助将不胜感激。
回答by aioobe
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 上应该运行良好。
回答by Charlie Collins
While URL.openStream will work, you would be better off using the Apache HttpClient library that comes with Android for HTTP. Among other reasons, you can use content encoding (gzip) with it, and that will make text file transfers much smaller (better battery life, less net usage) and faster.
虽然 URL.openStream 可以工作,但最好使用 Android 随附的 Apache HttpClient 库进行 HTTP。除其他原因外,您可以将内容编码 (gzip) 与它一起使用,这将使文本文件传输更小(更长的电池寿命,更少的网络使用量)和更快。
There are various ways to use HttpClient, and several helpers exist to wrap things and make it easier. See this post for more details on that: Android project using httpclient --> http.client (apache), post/get method(and note the HttpHelper I included there does use gzip, though not all do).
有多种使用 HttpClient 的方法,并且存在几个帮助器来包装事物并使其更容易。有关更多详细信息,请参阅这篇文章:Android 项目使用 httpclient --> http.client (apache),post/get 方法(并注意我在那里包含的 HttpHelper 确实使用了 gzip,但并非全部使用)。
Also, regardless of what method you use to retrieve the data over HTTP, you'll want to use AysncTask(or Handler) to make sure not to block the UI thread while making the network call.
此外,无论您使用什么方法通过 HTTP 检索数据,您都需要使用AysncTask(或 Handler)来确保在进行网络调用时不会阻塞 UI 线程。
And note that you should pretty much NEVER just use URL.openStream (without setting some configuration, like timeouts), though many examples show that, because it will block indefinitely if you the server is unavailable (by default, it has no timeout): URL.openStream() Might Leave You Hanging.
请注意,您几乎永远不应该只使用 URL.openStream(不设置一些配置,如超时),尽管许多示例表明,因为如果您的服务器不可用,它将无限期地阻塞(默认情况下,它没有超时):URL.openStream() 可能会让你束手无策。
回答by baron_bartek
Don't forget to add internet permissions to the manifest when taking net resources: (add in manifest).
获取网络资源时,不要忘记向清单添加互联网权限:(在清单中添加)。