从Android上的服务器读取文本文件 [英] Reading Text File From Server on Android

查看:185
本文介绍了从Android上的服务器读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有一个文本文件。我想从我的Android应用程序打开文本文件,然后在TextView中显示文本。我找不到任何关于如何与服务器进行基本连接并将数据馈送到String的示例。



您可以提供的任何帮助将不胜感激。 >

解决方案

尝试以下操作:

 尝试{
//为所需页面创建一个URL
URL url =新URL(mysite.com/thefile.txt);

//读取服务器返回的所有文本
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while((str = in.readLine())!= null){
// str是文本的一行; readLine()删除换行符
}
in.close();
} 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆