我想从我的服务器读取我的程序的待办事项列表。
我尝试过使用 BufferedReader
和 InputStream
,但还没有完全掌握它的窍门。
网址是:http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo
非常感谢任何帮助。
最佳答案
这是我创建的一个函数,它使用从url.openStream()
获得的InputStream。它将页面作为字符串返回。您可以稍后处理该页面。
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();
}
}
使用以下方式调用此函数:
getpage(new URL("http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo"));
关于java - 从远程文件读取字符串 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313934/