简体   繁体   English

从远程文件读取字符串-Java

[英]Reading strings from remote file - Java

I'd like to read my program's todo-list from my server. 我想从服务器上读取程序的待办事项清单。 I've tried using BufferedReader and InputStream , but haven't quite got the hang of it, yet. 我已经尝试使用BufferedReaderInputStream ,但是还没有完全掌握。

The URL is: http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo 网址为: http : //team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo

Any help is much appreciated. 任何帮助深表感谢。

This is a function created by me, it uses InputStream obtained from url.openStream() . 这是我创建的函数,它使用从url.openStream()获得的InputStream It returns the page as a String. 它以字符串形式返回页面。 You can process the page afterwards. 您可以随后处理该页面。

public String getpage(URL url)
    {
        try {
            // try opening the URL
            URLConnection urlConnection = url.openConnection();                        
            urlConnection.setAllowUserInteraction(false);

            InputStream urlStream = url.openStream();            
            byte buffer[] = new byte[1000];
            int numRead = urlStream.read(buffer);
            String content = new String(buffer, 0, numRead);

            while ((numRead != -1) && (content.length() < MAX_PAGE_SIZE)) {
                numRead = urlStream.read(buffer);
                if (numRead != -1) {
                    String newContent = new String(buffer, 0, numRead);
                    content += newContent;
                }
            }
            return content;
        } catch (IOException e) {            
            e.printTrackStace();
        }catch(IndexOutOfBoundsException e1){            
            e1.printTrackStace();
        }
    }

Call this function using: 使用以下命令调用此函数:

getpage(new URL("http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM