从URL获取csv并将其解析为ArrayList

时间:2018-02-07 11:32:52

标签: java android csv arraylist android-manifest

我正在用java和android手机制作股票模拟器应用程序。我试图从URL获取一个csv文件并将列解析为不同的ArrayLists但它一直崩溃。我为它制作了一个类,并在清单中添加了2行,因为我认为这是崩溃的原因:

我的清单中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

我的代码:

public class StockALLDownloader {
    public static final String LOG_TAG = "com.example.someone.stocksimulator.StockALLDownloader";
    String url = "https://api.iextrading.com/1.0/ref-data/symbols?format=csv";

    private ArrayList<String> symbols;
    private ArrayList<String> names;

    public ArrayList<String> getSymbols() {
        return symbols;
    }
    public ArrayList<String> getNames() {
        return names;
    }

    public StockALLDownloader(){ //gets url and adds to the symbols and names arrays
        try{
            URL iexfin = new URL(url);
            URLConnection data = iexfin.openConnection();
            Scanner input = new Scanner(data.getInputStream());
            if (input.hasNext()){ //skip header line
                input.nextLine();
            }
            while (input.hasNextLine()){
                String line = input.nextLine();
                Scanner scanner  =  new Scanner(line);
                scanner.useDelimiter(",");
                symbols.add(scanner.next());
                names.add(scanner.next());
                scanner.close();
            }
            input.close();
        }
        catch (Exception e){
            Log.e(StockALLDownloader.LOG_TAG, "error stock all downloader");
        }
    }
}

0 个答案:

没有答案