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或""

android csv显示乱码问题,Android CSV解析器问题相关推荐

  1. python csv 中文乱码_python读写csv时中文乱码问题解决办法

    CSV是英文Comma Separate Values(逗号分隔值)的缩写,顾名思义,文档的内容是由 "," 分隔的一列列的数据构成的,可以使用excel和文本编辑器等打开.CSV ...

  2. java 读取csv文件乱码_java读取csv文件乱码怎么解决

    csv文件默认编码为ANSI,java读取CSV出现乱码主要是编码不一致问题.(推荐:java视频教程)DataInputStream in = new DataInputStream(new Fil ...

  3. android richtext显示html,RichText Android 平台下的富文本显示控件 @codeKK Android开源站...

    注意:此项目已不再维护 Android 平台下的富文本解析器 流式操作 低侵入性 依赖少,只依赖了disklrucache和support v4 支持 Html 和 Markdown 格式文本 支持图 ...

  4. python 写入csv 文件显示乱码_python 写入csv乱码问题解决方法

    需求背景 最近为公司开发了一套邮件日报程序,邮件一般就是表格,图片,然后就是附件.附件一般都是默认写到txt文件里,但是PM希望邮件里的附件能直接用Excel这种软件打开,最开始想保存为Excel,但 ...

  5. AndroidStudio_Build Out窗口显示乱码解决方案---Android原生开发工作笔记222

    直接说解决方案吧,我这个已经解决了. 直接双击按下shift按键,出来以后输入上面的字符 选择Actions标签,然后点击.第一个 会打开一个窗口 然后输入: -Dfile.encoding=UTF- ...

  6. java csv导出 乱码_java导出csv乱码解决方法介绍

    1.问题 将查询的数据以xls文件导出时(UTF-8编码),数据正常:但以CSV文件导出时,文件中的中文乱码,同样是UTF-8编码,改成GBK编码导出时,中文显示正常. 本以为问题解决,后面导出含拉丁 ...

  7. android内容显示不出来,android – listview不显示任何内容并隐藏数据

    我相信你的问题可能是因为你试图显示的项目多于屏幕上的空间.为了抵消这种影响,您可以使用 HorizontalScrollView,让您可以水平滚动以查看所有视图. 此方向属性也不适用于Relative ...

  8. android textview显示表情,在Android TextView中显示表情符号/情感图标

    我在Android TextView中显示表情符号图标时遇到一些问题 首先,我在这里找到了Unicode表情符号图标列表:http : //www.easyapns.com/category/just ...

  9. android+状态栏显示图标大全,Android应用图标在状态栏上显示实现原理

    一.前方 在研究<Android类似360,QQ管家那样的悬浮窗>突发奇想,想把应用的图标也显示到状态栏上,类似手机QQ,而有消息来时改变状态栏上的图标显示. 二.原理 其实很研究完后,才 ...

最新文章

  1. Keras【Deep Learning With Python】机器学习和线性回归
  2. FPGA_进阶篇开篇
  3. MSSQL系列之十五 全文索引
  4. 最新高级流量卡官网源码附教程
  5. 学习电商的第一个业务,发票管理,发票作废(发票作废为重)
  6. 无法访问 函数不正确
  7. excel中的stdev和stdevp的区别等系列
  8. 【定位原理揭秘第三期】室内定位技术原理揭秘
  9. Python自动化整理文件“大升级”,任意路径下文件,都给你整理的明明白白!...
  10. 计算机网络七年级教学设计,初中七年级信息技术《计算机网络和因特网》教学设计.docx...
  11. python代码画樱花落花-Python:绘制樱花树
  12. 一、什么是JavaWeb?
  13. VBA SolidWorks 二次开发 API ---从宏开始
  14. ai如何置入_ai图片(ai怎么把图片嵌入到图形里)
  15. java 高德地图路线规划_公交出行路线规划-出行路线规划-开发指南-Android 地图SDK | 高德地图API...
  16. unity3d录音播放
  17. Linux 第九章-系统进程和服务管理
  18. NFT价值及白皮书获取
  19. golang入门笔记—ES
  20. C++字符函数库 cctype

热门文章

  1. 替换掉(取消掉)pip freeze 生成的@ file:///格式,变为正常的==版本号
  2. android 拖拽gridview,Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
  3. 为什么用c语言程序中的if语句实现从1加到100最后的结果是负数,用C语言程式计算从1加到100的程式是怎样的?...
  4. java 线程死锁简单例子_java 多线程死锁详解及简单实例
  5. ABP之展现层(Datatables分页)
  6. Yum本地Rpm库设置
  7. GCD之线程挂起与恢复
  8. 图片圆角边框自适应宽高(深夜原创)
  9. php刷新onload(),JS页面刷新的方法总结
  10. matlab 可视化界面,Matlab?的可视化界面设计