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.
For the first part:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
For the second part, Harry is right, just follow his code, or use some libraries: http://commons.apache.org/sandbox/csv/
CSVReader reader = new CSVReader(** Insert your Reader here **);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
A better CSV parser handles quoted fields
import android.content.Context;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
private class StringDArray {
private String[] data=new String[0];
private int used=0;
public void add(String str) {
if (used >= data.length){
int new_size= used+1;
String[] new_data=new String[new_size];
java.lang.System.arraycopy( data,0,new_data,0,used);
data=new_data;
}
data[used++] = str;
}
public int length(){
return used;
}
public String[] get_araay(){
return data;
}
}
private Context context;
public CSVReader(Context context){
this.context=context;
}
public List read(InputStream inputStream){
List resultList = new ArrayList();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String csvLine;
final char Separator = ',';
final char Delimiter = '"';
final char LF = '\n';
final char CR = '\r';
boolean quote_open = false;
while ((csvLine = reader.readLine()) != null) {
//String[] row = csvLine.split(",");// simple way
StringDArray a=new StringDArray();
String token="";
csvLine+=Separator;
for(char c:csvLine.toCharArray()){
switch (c){
case LF: case CR:// not required as we are already read line
quote_open=false;
a.add(token);
token="";
break;
case Delimiter:
quote_open=!quote_open;
break;
case Separator:
if(quote_open==false){
a.add(token);
token="";
}else{
token+=c;
}
break;
default:
token+=c;
break;
}
}
if(a.length()>0 ) {
if(resultList.size()>0){
String[] header_row =(String[]) resultList.get(0);
if(a.length()>=header_row.length) {
String[] row = a.get_araay();
resultList.add(row);
}
}else{
String[] row = a.get_araay();
resultList.add(row);//header row
}
}
}
inputStream.close();
}catch (Exception e){
Toast.makeText(context,"Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
return resultList;
}
}
Usage
File file=new File(path);
CSVReader csvReader=new CSVReader(activity.this);
List csv=csvReader.read( new FileInputStream(file));
if(csv.size()>0){
String[] header_row =(String[]) csv.get(0);
if(header_row.length>1){
String col1=header_row[0];
String col2=header_row[1];
}
}
Toast.makeText(activity.this,csv.size() + " rows", Toast.LENGTH_LONG).show();
Sample data used
ID,Name
1,Test Item 1
"2","Test Item 2"
"3","Test , Item 3"
4,Test Item 4
Related
I have created this code but don't know why its not working. The code doesn't print all lines of the csvfile.
try{
File csvfile = new File(FullPath);
FileInputStream csvStream = new FileInputStream(csvfile);
BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
String line;
int iCount=0;
while ((line = in.readLine()) != null){
String[] RowData = line.split(",");
name[iCount] = RowData[0];
Toast.makeText(NewMessage.this, "CSV", 2000).show();
number[iCount] = RowData[1];
iCount++;
}
in.close();
Toast.makeText(NewMessage.this, "CSV Has uploaded", 2000).show();
}
catch(Exception e)
{
e.printStackTrace();
}
If the problem is that is not printing all lines of the file, you are overriding the array on loop. You should edit your question, because the array is working, the really problem is that the file wasn't printing all lines.
Here's the solution for name,number format:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Teste {
public static void main(String[] args) throws IOException {
Teste x = new Teste();
x.doTeste();
}
public void doTeste() throws IOException{
String fileName = "D:\\Projetos\\Testes\\src\\teste.txt";
BufferedReader in = null;
try{
File csvfile = new File(fileName);
FileInputStream csvStream = new FileInputStream(csvfile);
in = new BufferedReader(new InputStreamReader(csvStream));
String line;
String[] rowName = new String[(countLines(fileName)+1)];
String[] rowNameData = new String[(countLines(fileName)+1)];
int linha = 0;
while ((line = in.readLine()) != null){
String[] array = line.split(",");
rowName[linha] = array[0];
rowNameData[linha] = array[1];
++linha;
}
System.out.println("The rowName ARRAY");
for (String s: rowName){
System.out.println(s);
}
System.out.println("The rowNameData ARRAY");
for (String s: rowNameData){
System.out.println(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
in.close();
}
}
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
}
and for name in one line, and number the next line:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Teste {
public static void main(String[] args) throws IOException {
Teste x = new Teste();
x.doTeste();
}
public void doTeste() throws IOException{
String fileName = "D:\\Projetos\\Testes\\src\\teste.txt";
BufferedReader in = null;
try{
File csvfile = new File(fileName);
FileInputStream csvStream = new FileInputStream(csvfile);
in = new BufferedReader(new InputStreamReader(csvStream));
String line;
String[] rowName = new String[(countLines(fileName)+1)/2];
String[] rowNameData = new String[(countLines(fileName)+1)/2];
int iCount=0;
int x = 0;
int y = 0;
while ((line = in.readLine()) != null){
if (iCount == 0 || iCount%2 == 0) {
rowName[x] = line;
++x;
}
else {
rowNameData[y] = line;
++y;
}
iCount++;
}
System.out.println("The rowName ARRAY");
for (String s: rowName){
System.out.println(s);
}
System.out.println("The rowNameData ARRAY");
for (String s: rowNameData){
System.out.println(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
in.close();
}
}
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
}
I'm trying to parse some XML to a string and I'm getting an outofbounds exception. I'm fairly new to android as well as trying to get text from a website, namely the CTA Bus Tracker API . One block of the XML looks like this:
<route>
<rt>1</rt>
<rtnm>Bronzeville/Union Station</rtnm>
</route>
This is my method:
class loadRoutes extends AsyncTask<String, String, String[]> {
#Override
protected String[] doInBackground(String... strings) {
try {
URL routesURL = new URL(strings[0]);
BufferedReader in = new BufferedReader(new InputStreamReader(routesURL.openStream()));
String [] result = new String[2];
String line;
while((line = in.readLine()) != null) {
if(line.contains("<rt>")) {
int firstPos = line.indexOf("<rt>");
String tempNum = line.substring(firstPos);
tempNum = tempNum.replace("<rt>", "");
int lastPos = tempNum.indexOf("</rt>");
result[0] = tempNum.substring(0, lastPos);
in.readLine();
firstPos = line.indexOf("<rtnm>");
String tempName = line.substring(firstPos);
tempName = tempName.replace("<rtnm>", "");
lastPos = tempName.indexOf("</rtnm>");
result[1] = tempName.substring(0, lastPos);
}
}
in.close();
return result;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
The first readline() gets to the line with an rt and grabs that line, then in the if statement, readline() should get the next line, which should contain rtnm. I keep getting indexoutofbounds on the line firstPos = line.indexOf("rtnm").
The while loop is already reading in the next line, so you don't need to in.readLine(); in the if statement. Try running it like this:
while((line = in.readLine()) != null) {
if(line.contains("<rt>")) {
int firstPos = line.indexOf("<rt>");
String tempNum = line.substring(firstPos);
tempNum = tempNum.replace("<rt>", "");
int lastPos = tempNum.indexOf("</rt>");
result[0] = tempNum.substring(0, lastPos);
} else if (line.contains("<rtnm>") {
firstPos = line.indexOf("<rtnm>");
String tempName = line.substring(firstPos);
tempName = tempName.replace("<rtnm>", "");
lastPos = tempName.indexOf("</rtnm>");
result[1] = tempName.substring(0, lastPos);
}
}
Also, it might be easier to write your own XML parser in a different class. This XML parser android documentation has an example of exactly what you are trying to do.
I am trying to parse Vcard file. here is my code.
public void get_vcf_data(String file) throws VCardException, IOException{
VCardParser parser = new VCardParser();
VDataBuilder builder = new VDataBuilder();
//String file = path;
//read whole file to string
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));
String vcardString = "";
String line;
while ((line = reader.readLine()) != null) {
vcardString += line + "\n";
}
reader.close();
//parse the string
boolean parsed = parser.parse(vcardString, "UTF-8", builder);
if (!parsed) {
throw new VCardException("Could not parse vCard file: " + file);
}
//get all parsed contacts
List<VNode> pimContacts = builder.vNodeList;
//do something for all the contacts
for (VNode contact : pimContacts) {
ArrayList<PropertyNode> props = contact.propList;
//contact name - FN property
String name = null;
String number = null;
String tel = null;
for (PropertyNode prop : props) {
if ("FN".equals(prop.propName)) {
name = prop.propValue;
Contact_name.add(name);
Log.d("Name", name);
//we have the name now
break;
}
}
for (PropertyNode prop : props) {
if ("N".equals(prop.propName)) {
number = prop.propValue;
Contact_number.add(number);
Log.d("Name", number);
//we have the name now
break;
}
}
for (PropertyNode prop : props) {
if(" TEL".equals(prop.propName))
{
tel = prop.propValue;
Contact_tel.add(tel);
Log.d("Name", tel);
}
}
Log.d("Tag", ""+Contact_name.size()+"::"+Contact_number.size()+"::"+Contact_tel.size());
//similarly for other properties (N, ORG, TEL, etc)
//...
System.out.println("Found contact: " + name);
}
}
but facing problem in while loop
while ((line = reader.readLine()) != null) {
vcardString += line + "\n";
}
continuously looping inside a while loop and doesn't exit from loop once it is entered
I want to make epub reader app.Now i am getting only chapter name in the file but how to get whole data in the chapter.
I think I have already posted this out before.
Using nl.siegmann.epublib which you can google.
In my code I will show you how I did it as you look at Book class which shows how the the epub works.
Using Spine on book class I get the maximum spine of the book which means the entire book.
I then convert it to string.
Here is my code on how I did it.
public String getEntireBook()
{
String line, linez = null;
Spine spine = amBook().getSpine();
Resource res;
List<SpineReference> spineList = spine.getSpineReferences() ;
int count = spineList.size();
int start = 0;
StringBuilder string = new StringBuilder();
for (int i = start; count > i; i = i +1) {
res = spine.getResource(i);
try {
InputStream is = res.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((line = reader.readLine()) != null) {
linez = string.append(line + "\n").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
return linez;
}
I have a String that I try to split. The following code works
lsSagor = "some text\n Some more text\n More text~Text again\n Text\n text~Some text ..."
final String[] laList = lsSagor.split("~");
String[] laSaga = laList[0].split("\n");
Gives:
laSaga[0] => some text
laSaga[1] => some more text
laSaga[2] => More text
But if I download the textfile, it fails to split and gives:
laSaga[0] => "some text\n Some more text\n More text"
So it seems the first split works, but not the second.
Here is the code I use to download the file
String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt");
public static String getFileFromUrl(String url)
{
InputStream content = null;
try
{
HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();
}
catch (Exception e)
{
//handle the exception !
}
BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
From the documentation
I don't think you will find your string contains any newline character to split on, you would need to do
while ((line = rd.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
to get that and I'm sure there is an easier way to just read it newlines and all in the first place.
Hi I think the problem is in String.split() function
Old method but work :)
public static String[] splitString(String str, char separator)
{
String[] retVal = null;
int length = str.length();
int size = 1;
int jIndx = 0;
int expressionLength = 0;
while ((jIndx = str.indexOf(separator, jIndx + 1)) != -1)
{
size++;
}
retVal = new String[size];
jIndx = 0;
char[] charArray = str.toCharArray() ;
for (int index = 0; index < length; index++)
{
if (charArray[index] == separator)
{
retVal[jIndx] = str.substring(index - expressionLength, index);
jIndx++;
expressionLength = 0;
}
else
expressionLength++;
if (index + 1 == length)
{
retVal[jIndx] = str.substring(index + 1 - expressionLength, index + 1);
}
}
return retVal;
}
This is the (not so beautiful) solution
lsSagor = "some text# Some more text# More text~Text again\n Text# text~Some text ..."
String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt");
final String[] laList = lsSagor.split("~");
giAntalSagor = laList.length;
String[] laSaga = laList[0].split("#");
final String[] guiLaList = new String[giAntalSagor];
for (int i = 0; i < giAntalSagor; i++)
{
guiLaList[i] = laList[i].replaceAll("#", "\n");
}
guiLaList is used for layout with "\n" and the other list laList to get the information I wanted.