尝试将提取的xml文件从一个类解析到另一个类

时间:2018-11-20 23:17:04

标签: java xml parsing

我正在将android studio用于一个学校项目,并且现在处于最后阶段。我剩下的唯一一件事是,一个班级有2个微调器,一个确定程序,另一个确定学期,然后是一个按钮,该按钮将接这两个微调器,并吐出一个与该程序时间表有关的URL,学期。问题是,我不知道该如何使用该吐出的URL,并将其解析为另一个显示时间表的java类。在下面,您将找到附上的代码,该代码可以正常工作,但由于其硬编码而无法显示正确的时间表

   StringBuffer sb = new StringBuffer();
                sb.append("http://branko-cirovic.appspot.com/etcapp/timetables/timetable_"); sb.append(cid); sb.append(semester); sb.append(".xml");
                loc = sb.toString();

                Toast.makeText(ScheduleMainActivity.this,"You Selected : "
                        + loc, Toast.LENGTH_SHORT).show();

                Intent toy7 = new Intent(ScheduleMainActivity.this, TimetableMainActivity.class);
                toy7.putExtra("Name", loc);
                startActivity(toy7);

这是即时通讯创建意图并使用putExtra在下一个活动中使用数据的活动

 public class GetXML extends AsyncTask<String, Void, String> {
    String src = null;
    String loc = (String) getIntent().getExtras().get("Name");

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL(loc);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            src = readStream(con.getInputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return src;
    }

    @Override
    protected void onPostExecute(String result) {
        if (src == null)
            new AlertDialog.Builder(TimetableMainActivity.this).
                    setTitle("Error").setMessage("No Schedule Found").
                    setNeutralButton("Close", null).show();
        else {
            parseXML(src);
        }

        setContentView(R.layout.activity_main_timetable);

        days = new String[5][10];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 10; j++) {
                int k = i * 10 + j;
                days[i][j] = schedule.get(k);
            }
        }

        for (int i = 0; i < 5; i++)
            list[i] = new ArrayList<Map<String, String>>();

        int count = hours.length;

        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < count; i++) {
                map = new HashMap<String, String>();
                map.put("time", hours[i]);
                map.put("description", days[j][i]);
                list[j].add(map);
            }
        }

        Calendar cal = Calendar.getInstance();
        int today = cal.get(Calendar.DAY_OF_WEEK) - 2;

        pos = 0;
        if (today >= 0 && today <= 4)
            pos = today;

        ViewPager viewPager = findViewById(R.id.ViewPager);
        CustomPagerAdapter adapter = new CustomPagerAdapter(TimetableMainActivity.this, list);
        PagerTabStrip pagerTabStrip = findViewById(R.id.pager_tab);

        int color = Color.parseColor("#33b7ee");
        pagerTabStrip.setTabIndicatorColor(color);

        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(pos);
    }

这是我收到错误的地方。我收到一个IndexOut of bounds错误,并且应用程序崩溃了

2018-11-20 22:35:19.126 20681-20681/com.example.mr_ru.listview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mr_ru.listview, PID: 20681
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.example.mr_ru.listview.TimetableMainActivity$GetXML.onPostExecute(TimetableMainActivity.java:100)
    at com.example.mr_ru.listview.TimetableMainActivity$GetXML.onPostExecute(TimetableMainActivity.java:66)
    at android.os.AsyncTask.finish(AsyncTask.java:695)
    at android.os.AsyncTask.access$600(AsyncTask.java:180)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是错误btw

0 个答案:

没有答案