English 中文(简体)
阅读文本文档
原标题:Reading Text File From Server on Android

我的服务器上有一个文件。 我想从我的安伯斯开放文本档案,然后在文函中显示案文。 我无法找到任何实例,说明如何与服务器进行基本联系,并将数据输入“优势”。

谨请你提供任何帮助。

最佳回答

如下:

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) {
}

(摘自Exampledepot: 摘自URL

最好在安康开展工作。

问题回答

虽然URL. openStream将工作,但你会更好地利用爱琴蒂项目图书馆。 除其他原因外,你可以使用内容编码(gzip),并将使文本的转让更加少(改善电池生活,减少净使用)。

采用HttpClient的方式多种多样,而且有几个帮助者可以总结情况,使之更容易。 See this post for more details on that: Android Project using httpclient --> http://client.(apache), post/getmeth (并且指出,HttpHelper I包括在那里确实使用gzip,但并非全部使用)。

此外,无论你为检索吉大港山区的数据采用了何种方法,你都希望使用AysncTask(或 Handler),以确保不阻挡地安纳热线,同时发出网络电话。

并且指出,你应当只使用URL. openStream(不像时间说明那样确定某种配置),尽管许多例子显示,如果你无法提供服务器,就会无限期地阻止(由于缺席,它没有时间:URL. openStream(电话) Might 离开You Banging

不要忘记,在获得净资源时,会增加互联网上的许可:(具体表现)。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签