Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get and Parse CSV file in android

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);             } 
like image 862
locoboy Avatar asked Mar 19 '11 06:03

locoboy


People also ask

How do I read and parse a CSV file in Android?

Android by default does not create the raw folder. Create a raw folder under res/raw in your project. copy your CSV File in that. keep the name of the CSV file lower case and convert into text format when asked.

Where do CSV files go on android?

You can use it to store data in a table structured format. For example, save following table to app/src/main/assets/movies. csv file of the project.

What is parse in CSV?

The csv-parse package is a parser converting CSV text input into arrays or objects. It is part of the CSV project. It implements the Node. js stream.


1 Answers

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.

like image 116
Harry Joy Avatar answered Oct 14 '22 14:10

Harry Joy