java - Android Studio : Save URLS from a URL to an array

标签 java android arrays android-studio

我试图让 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。如果有人对如何获得此内容有任何帮助,我们将不胜感激。

最佳答案

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new MyAsyncFileReader().execute("http://www.example.com/my/website/directory/thefile.txt");

}

public static class MyAsyncFileReader extends AsyncTask<String, Void,String []>
{

    @Override
    protected String [] doInBackground(String... params){
        String url= params[0];
        List<String> lines = new ArrayList<String>();
        try{
            URL u= new URL(url);

            HttpURLConnection conn= (HttpURLConnection) u.openConnection();
            InputStream is= conn.getInputStream();

            BufferedReader br =new BufferedReader(new      InputStreamReader(is));
            String line=null;
            while((line=br.readLine())!=null){
                lines.add(line);

            }
            br.close();
        }catch(Exception e){

        }
        return lines.toArray(new String[lines.size()]);

    }

    @Override
    protected void onPostExecute(String[] strings) {
        //You will get your string array result here .
        // do whatever operations you want to do

         for(int i=0;i<strings.length;i++){
            Log.e("LOG", strings[i]);
        }
    }

}

}

这就是它在“我的 Activity ”中的样子。在您的 Activity 中使用类似的方法。

我的编辑:将 MyAsyncFileReader 类设为公共(public)和静态,以便通过其他 Activity 读取。

关于java - Android Studio : Save URLS from a URL to an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34688810/

相关文章:

我可以在循环中定义一个数组,其大小在每次迭代中都不同吗

php - array_replace_recursive 不会用一些空数组替换数组

java - 有没有更快的方法从 Sqlite 查询中遍历行?

java - java中静态变量的问题

java - 需要启用/禁用 CacheConnection 的 oracle JDBC 驱动程序的数据源

android - Mapview 显示空白但 fragment 正常

c++ - 释放二维数组中的内存

java - 当该部分文本发生更改时,从“可编辑”中删除跨度

java - 如何在 Volley 进行时制作应用程序 "sleep"

java - 在 Java 中将 Firestore 中的字符串保存到 ArrayList 中