获取并解析CSV文件中的android [英] Get and Parse CSV file in android

查看:367
本文介绍了获取并解析CSV文件中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个CSV文件<一个href="http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2">http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2然后分析它,这样我可以得到的价格,价格变成了一个对象,设置这两个属性。有没有一种方法,我可以用android库做到这一点?

编辑:这是工会(不工作)的当前状态:

  HttpClient的HttpClient的=新DefaultHttpClient();
        HttpContext的localContext =新BasicHttpContext();
        HTTPGET HTTPGET =新HTTPGET(URI);
        HTT presponse响应= httpClient.execute(HTTPGET,localContext);
        字符串结果=;

        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));

        串线= NULL;
        而((行= reader.readLine())!= NULL){
              结果+ =行+\ N的;
              的String [] RowData = result.split(\ N);
              字符串名称= RowData [0];
              串价格= RowData [1];
              字符串的变化= RowData [2];

              stock.setPrice(Double.parseDouble(价格));
              stock.setTicker(名称);
              stock.setChange(变化);
            }
 

解决方案

尝试是这样的:

  // ---假设你有输入流`is` CSV文件的话:

    的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
    尝试 {
        串线;
        而((行= reader.readLine())!= NULL){
             串[] RowData = line.split(,);
             日期= RowData [0];
             值= RowData [1];
            //做一些与数据和价值
        }
    }
    赶上(IOException异常前){
        //处理异常
    }
    最后 {
        尝试 {
            is.close();
        }
        赶上(IOException异常E){
            //处理异常
        }
    }
 

希望这有助于。

I'm trying to get a csv file from http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2 then parse it so that I can get the price and the price changed into an object that sets both properties. Is there a way that I can do this with the android libraries?

Edit: Here's the current state of the union (not working):

HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = null;
        while ((line = reader.readLine()) != null){
              result += line + "\n";
              String[] RowData = result.split("\n");
              String name = RowData[0];
              String price = RowData[1];
              String change = RowData[2];

              stock.setPrice(Double.parseDouble(price));
              stock.setTicker(name);
              stock.setChange(change);
            }

解决方案

Try something like this:

    //--- Suppose you have input stream `is` of your csv file then:

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    try {
        String line;
        while ((line = reader.readLine()) != null) {
             String[] RowData = line.split(",");
             date = RowData[0];
             value = RowData[1];
            // do something with "data" and "value"
        }
    }
    catch (IOException ex) {
        // handle exception
    }
    finally {
        try {
            is.close();
        }
        catch (IOException e) {
            // handle exception
        }
    }

Hope this helps.

这篇关于获取并解析CSV文件中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆