【发布时间】:2016-04-13 19:55:39
【问题描述】:
我正在尝试让 Android Studio 将文本文件从 URL 读取到字符串数组中。然后,使用 Picasso 中的 URL 来显示一个简单的画廊。下面是我实现这一目标的尝试。
我的GrabData.java 脚本:
public class GrabData
{
static public String[] LIST = {};
public static void main(String[] args)
{
ReadFile rf = new ReadFile();
// The text file.
String filename = "http://www.example.com/my/website/directory/thefile.txt";
try
{
String[] LIST = rf.readLines(filename);
for (String line : LIST)
{
System.out.println(LIST);
}
}
catch(IOException e)
{
System.out.println("ReadFile : Unable to create "+filename+": "+e.getMessage());
}
}
}
我的ReadFile.java 脚本:
public class ReadFile extends Activity
{
public String[] readLines(String filename) throws IOException
{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
}
但是,当我尝试在文件名字符串中使用 URL 时,它不会在可以从数组加载的图库中显示任何图像。我猜这个方法不能读取 URLS。如果有人对如何获得这个有任何帮助,非常感谢。
【问题讨论】:
-
也发布您的画廊网址
-
FileReader无法通过 http 读取文件,您需要HTTPUrlConnection才能读取。
标签: java android arrays android-studio