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); }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With