获取然后将CSV行数据导出到字符串(android)

时间:2014-06-28 05:34:10

标签: android string csv sharedpreferences http-get

我有一个csv文件,我的应用程序从互联网上下载,然后解析。但每当我实际解析csv文件时,我的app力就会在第34行Band = RowData [1];"以及第1行出现错误。我以前从未处理过CSV文件因此,如果有人可以告诉我如何正确获取CSV文件的行数据并将行数据转换为字符串,将会很有帮助。以下是CSV文件的位置:http://www.beaconschool.org/~markovic/lincoln.php

我该怎么做才能读取我的CSV文件中的所有内容并将所有内容添加到共享首选项文件中?

此外,它似乎有点工作,因为它产生这个共享首选项文件,其中包含以下内容

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="title">""</string>
    <string name="band">"ZQ11ISF1"</string>
    <string name="description">""<br>""</string>
    <string name="data">""</string>
    <string name="class">"INSTRUCTIONAL SKILLS"</string>
    <string name="number">"1"</string>
    <string name="teacher">"MARKOVIC"</string>
    <string name="date">""</string>
    <string name="type">""</string>
</map>

以下是我的应用中的代码,用于获取并解析CSV文件,尝试将行数据转换为字符串

HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://www.beaconschool.org/~markovic/lincoln.php");
    HttpResponse response = httpClient.execute(httpGet, localContext);
    String result = "";



    try {
        Log.d("receiver", "animation stopped and downloaded file");
        BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          response.getEntity().getContent()

                )


                );


            String line;
            while ((line = reader.readLine()) != null) {
                 String[] RowData = line.split(",");
                 Data = RowData[0];
                 Band = RowData[1];
                 Number = RowData[2];
                 Class = RowData[3];
                 Teacher = RowData[4];
                 Title = RowData[5];
                 Date = RowData[6];
                 Type = RowData[7];
                 Description = RowData[8];


                 SharedPreferences.Editor localEditor = getActivity().getSharedPreferences("due_today", Context.MODE_PRIVATE ).edit();

                 localEditor.putString("data", Data.toString());
                 localEditor.putString("band", Band.toString());
                 localEditor.putString("number", Number.toString());
                 localEditor.putString("class", Class.toString());
                 localEditor.putString("teacher", Teacher.toString());
                 localEditor.putString("title", Title.toString());
                 localEditor.putString("date", Date.toString());
                 localEditor.putString("type", Type.toString());
                 localEditor.putString("description", Description.toString());

              localEditor.apply();

            Log.d("receiver", "information given to shared preferences");

            swipeLayout.setRefreshing(false);

            }

这是logcat

06-28 01:21:24.662: W/dalvikvm(7808): VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
06-28 01:21:29.442: W/dalvikvm(7808): threadid=11: thread exiting with uncaught exception (group=0x41665ce0)
06-28 01:21:29.452: E/AndroidRuntime(7808): FATAL EXCEPTION: AsyncTask #1
06-28 01:21:29.452: E/AndroidRuntime(7808): Process: com.bernard.beaconportal, PID: 7808
06-28 01:21:29.452: E/AndroidRuntime(7808): java.lang.RuntimeException: An error occured while executing doInBackground()
06-28 01:21:29.452: E/AndroidRuntime(7808):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.lang.Thread.run(Thread.java:841)
06-28 01:21:29.452: E/AndroidRuntime(7808): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
06-28 01:21:29.452: E/AndroidRuntime(7808):     at com.bernard.beaconportal.Due_Today_Fragment$Update.doInBackground(Due_Today_Fragment.java:232)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at com.bernard.beaconportal.Due_Today_Fragment$Update.doInBackground(Due_Today_Fragment.java:1)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-28 01:21:29.452: E/AndroidRuntime(7808):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-28 01:21:29.452: E/AndroidRuntime(7808):     ... 4 more

2 个答案:

答案 0 :(得分:1)

因此,当RowData的大小为1时,您正在访问第二项:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

分割后,您确定自己的RowData大小正确吗?

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

在检索RowData

的项目之前进行检查
if (RowData.length > 1) {
    Band = RowData[1];
}

对其他作业也这样做。

答案 1 :(得分:1)

为了避免ArrayIndexOutOfBoundsException,您可以尝试这样的事情!

String RowData[] = line.split(",");
int noOfItems = RowData.length;
int counter=0;
Data =  counter<noOfItems ? RowData[counter] : "";
counter++;
Band =  counter<noOfItems ? RowData[counter] : "";
counter++;
Number =  counter<noOfItems ? RowData[counter] : "";
counter++;
Class =  counter<noOfItems ? RowData[counter] : "";
counter++;
Teacher =  counter<noOfItems ? RowData[counter] : "";
counter++;
Title =  counter<noOfItems ? RowData[counter] : "";
counter++;
Date =  counter<noOfItems ? RowData[counter] : "";
counter++;
Type =  counter<noOfItems ? RowData[counter] : "";
counter++;
Description =  counter<noOfItems ? RowData[counter] : "";

上面的代码将检查数组是否包含值,并给出rowData的值(如果可用)。否则,它将为字符串分配一个null(“”)值。空值可以用任何适当的值替换,例如“No Data Available”或“NA”等。