android csv显示乱码问题,Android CSV解析器问题

5 个答案:

答案 0 :(得分:2)

为什么要编写自己的CSV解析,当你可以使用已经编写过的库来执行此操作时?也许OpenCSV可以帮助您实现CSV解析目标。

答案 1 :(得分:2)

您想要这样做或类似的事情:

String date = null, value = null;

String[] RowData = line.split(",");

date = RowData[0];

if(RowData.length ==2)value = RowData[1]; (this is the row it crashes on)

或者它的一些变化,例如if(RowData.length< 2)不要尝试读取该值。它是一个非常标准的东西 - 如果你向一个数组索取一个值的索引它没有Java会崩溃。

答案 2 :(得分:1)

public class CityParser {

DocumentBuilderFactory factory;

DocumentBuilder builder;

Document doc;

Element ele;

int mediaThumbnailCount;`enter code here`

boolean urlflag;

CityListBean objBean = null;

Vector vecCityList;

public CityParser() {

}

public Vector getCityData() {

vecCityList = new Vector();

try {

HttpClient httpClient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

HttpGet httpGet = new HttpGet(

"http://heresmyparty.com/cms/index.php?option=com_chronocontact&chronoformname=add_event_form_download");

HttpResponse response = httpClient.execute(httpGet, localContext);

// String result = "";

BufferedReader reader = new BufferedReader(new InputStreamReader(

response.getEntity().getContent()));

CSVReader csvreader = new CSVReader(reader);

String[] nextLine;

while ((nextLine = csvreader.readNext()) != null) {

CityListBean objcitylist = new CityListBean();

// nextLine[] is an array of values from the line

objcitylist.setText_title(nextLine[5]);

objcitylist.setText_host(nextLine[6]);

objcitylist.setText_price(nextLine[7]);

objcitylist.setDate(nextLine[8]);

objcitylist.setText_venue(nextLine[11]);

objcitylist.setAddress(nextLine[12]);

objcitylist.setLatitude(nextLine[13]);

objcitylist.setLongitude(nextLine[14]);

objcitylist.setFile(nextLine[15]);

objcitylist.setText_description(nextLine[16]);

objcitylist.setCity(nextLine[17]);

vecCityList.addElement(objcitylist);

}

/*for (int i = 0; i < vecCityList.size(); i++) { CityListBean

objcity = (CityListBean) vecCityList.get(i);

System.out.println("Cf_id : " + objcity.getCityName());

System.out.println("-----------------------------------"); }*/

} catch (Exception ex) {

ex.printStackTrace();

}

return vecCityList;

}

}

==========================================================================================

public class CSVReader {

private BufferedReader br;

private boolean hasNext = true;

private char separator;

private char quotechar;

private int skipLines;

private boolean linesSkiped;

public static final char DEFAULT_SEPARATOR = ',';

public static final char DEFAULT_QUOTE_CHARACTER = '"';

public static final int DEFAULT_SKIP_LINES = 0;

public CSVReader(Reader reader) {

this(reader, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,

DEFAULT_SKIP_LINES);

}

public CSVReader(Reader reader, char separator, char quotechar, int line) {

this.br = new BufferedReader(reader);

this.separator = separator;

this.quotechar = quotechar;

this.skipLines = line;

}

public String[] readNext() throws IOException {

String nextLine = getNextLine();

return hasNext ? parseLine(nextLine) : null;

}

private String getNextLine() throws IOException {

if (!this.linesSkiped) {

for (int i = 0; i < skipLines; i++) {

br.readLine();

}

this.linesSkiped = true;

}

String nextLine = br.readLine();

if (nextLine == null) {

hasNext = false;

}

return hasNext ? nextLine : null;

}

private String[] parseLine(String nextLine) throws IOException {

if (nextLine == null) {

return null;

}

List tokensOnThisLine = new ArrayList();

StringBuffer sb = new StringBuffer();

boolean inQuotes = false;

do {

if (inQuotes) {

// continuing a quoted section, reappend newline

sb.append("\n");

nextLine = getNextLine();

if (nextLine == null)

break;

}

for (int i = 0; i < nextLine.length(); i++) {

char c = nextLine.charAt(i);

if (c == quotechar) {

// this gets complex... the quote may end a quoted block, or escape another quote.

// do a 1-char lookahead:

if( inQuotes // we are in quotes, therefore there can be escaped quotes in here.

&& nextLine.length() > (i+1) // there is indeed another character to check.

&& nextLine.charAt(i+1) == quotechar ){ // ..and that char. is a quote also.

// we have two quote chars in a row == one quote char, so consume them both and

// put one on the token. we do *not* exit the quoted text.

sb.append(nextLine.charAt(i+1));

i++;

}else{

inQuotes = !inQuotes;

// the tricky case of an embedded quote in the middle: a,bc"d"ef,g

if(i>2 //not on the begining of the line

&& nextLine.charAt(i-1) != this.separator //not at the begining of an escape sequence

&& nextLine.length()>(i+1) &&

nextLine.charAt(i+1) != this.separator //not at the end of an escape sequence

){

sb.append(c);

}

}

} else if (c == separator && !inQuotes) {

tokensOnThisLine.add(sb.toString());

sb = new StringBuffer(); // start work on next token

} else {

sb.append(c);

}

}

} while (inQuotes);

tokensOnThisLine.add(sb.toString());

return (String[]) tokensOnThisLine.toArray(new String[0]);

}

public void close() throws IOException{

br.close();

}

}

答案 3 :(得分:1)

在尝试访问RowData之前检查它的长度。看起来split()返回一个带有单个对象的数组,但是你试图访问第二个对象,这确实是超出范围的。

答案 4 :(得分:0)

您是否尝试过先检查

if (RowData[1]!=null) or possibly if (RowData[1]!="")

我不明白为什么会导致你的应用崩溃,

它应该将值设置为null或""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值