0

在循环中,“m”从 0 到 4 工作,但随后崩溃,而不是从 0 到 6。

ArrayList<ItemTeacher> results = new ArrayList<ItemTeacher>();
String resultteacher="";
String[] binku2=null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("http://"+localip+":8080/Apj/Teacher_Servlet");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
         sb.append(line);
    }
    is.close();
    resultteacher = sb.toString();
    System.out.println("RESULT is : -**********"+resultteacher+"********-");


    String[] binku = resultteacher.split("@");

    System.out.println("LENGTH OF BINKU = "+binku.length);

    for(int m=0;m<=binku.length-1;m++)
    {

        binku2 = binku[m].split("#");
        System.out.println("Record number"+binku2[m]);
        drawable = LoadImageFromWebOperations("http://"+localip+":8080/Apj/TeacherImage/"+binku2[4]+".png");
        System.out.println("IMAGE DATA DISPLAY ="+drawable);

        ItemTeacher item_teacher = new ItemTeacher();

        item_teacher.setname(binku2[0]);
            item_teacher.setinfo(binku2[1]);
            item_teacher.setimage(drawable);
            String binku3[] = binku2[2].split("~");
            String binku4[] = binku2[3].split("~");

            String time="";
            String timeroom="";
            time =("Lecture Timings - Classroom");
            for(int i=0;i<=8;i++)
            {
            //  time +=("[Timings and Room]");
                timeroom +=("["+binku3[i]+" - "+binku4[i]+"]"+"      ");
                //System.out.println("Record number"+binku3[i]);
            }
            item_teacher.settime(time);
            item_teacher.setroom(timeroom);
            System.out.println("m value is ="+m);

            results.add(item_teacher);

    }
}


catch (Exception e) {
     System.out.println("Error parsing data " + e.toString());
}

return results;
}

日志详细信息:

02-12 21:33:36.772: I/System.out(358): RESULT is : -**********Mrs Roopali Sood#M.C.M (Head IT Dept)#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#F8~Lab 1~Lab 3~F7~F8~S9~Free~S8~F8~#1@Mr Jagmohan Mago#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#2@Mr Munish Gupta#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#3@Mrs Pallavi Chopra#M.C.A., M.Phil. (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~#4@Mr Abhejeet Singh#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 3~Lab 1~Lab 3~S7~F8~Lab 2~Free~S8~S9~#5@Vinay#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#6@Bharti#Pata nai#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#7@********-
02-12 21:33:36.784: I/System.out(358): LENGTH OF BINKU = 7
02-12 21:33:36.784: I/System.out(358): Record numberMrs Roopali Sood
02-12 21:33:37.082: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4618b990
02-12 21:33:37.102: I/System.out(358): m value is =0
02-12 21:33:37.102: I/System.out(358): Record numberM.C.A (IT Dept).
02-12 21:33:37.512: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4619c2a8
02-12 21:33:37.522: I/System.out(358): m value is =1
02-12 21:33:37.522: I/System.out(358): Record number9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~
02-12 21:33:37.772: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ac610
02-12 21:33:37.782: I/System.out(358): m value is =2
02-12 21:33:37.792: I/System.out(358): Record numberLab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~
02-12 21:33:38.102: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461bca98
02-12 21:33:38.112: I/System.out(358): m value is =3
02-12 21:33:38.112: I/System.out(358): Record number5
02-12 21:33:38.492: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ccc80
02-12 21:33:38.492: I/System.out(358): m value is =4
02-12 21:33:38.505: I/System.out(358): Error parsing data java.lang.ArrayIndexOutOfBoundsException
4

2 回答 2

2

这几乎肯定不是您想要的:

binku2 = binku[m].split("#");
System.out.println("Record number"+binku2[m]);

您似乎在任何地方都进行操作,就好像binku2数组的长度为 4,但随后您尝试访问值为 的索引m。因此,当您达到循环的第 5 次迭代时,mis 4 并且您抛出异常。可能您打算访问binku这里:

binku2 = binku[m].split("#");
System.out.println("Record number"+binku[m]);
于 2013-02-12T16:28:41.947 回答
1

您通过像这样对循环进行硬编码来为自己设置错误:

for(int i=0;i<=8;i++)

您应该始终使用动态值,例如m<=binku.length-1或更简单的版本m < binku.length。当您使用多个数组时,检查每个数组的长度是最安全的:

for(int i = 0; i < binku3.length && i < binku4.length; i++)
于 2013-02-12T16:28:19.357 回答