0

我的 Android 项目有问题。我正在尝试从 XML 文件中提取信息,该文件目前存储在 WAMP 项目文件中。但是每次我运行该项目时,都会出现 XmlPullParser 异常。我对不同的活动使用了相同的代码,它工作得很好。无法弄清楚错误。这是代码

public class MondayActivity extends Activity{

    static String LIST_URL = "http://192.168.1.10/monday.xml";
    ArrayList<HashMap<String, String>> timetableList;
    Handler mHandler;
    ClassesDownloadTask mTask;
    ProgressDialog mProgress;

    public Object onRetainNonConfigurationInstance(){
        Log.v("XML", "Saving data");
        return timetableList;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.v("HIT", "onCreate triggered");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.monday);
        loadTimetableLists();
        displayTimetableLists();

        mHandler = new Handler(){
            public void handleMessages(Message mgs){
                displayTimetableLists();
                mProgress.dismiss();
                if(mgs.what == 1){
                    new AlertDialog.Builder(MondayActivity.this)
                        .setTitle("Error")
                        .setPositiveButton(android.R.string.ok, null)
                        .setMessage("Failed to download packages")
                        .show();
                }else{
                    displayTimetableLists();
                }
            }
        };
        //startBackgroundTask();
        //displayTourPackages();
        timetableList = (ArrayList<HashMap<String,String>>)
        getLastNonConfigurationInstance();
        if(timetableList == null){
            startBackgroundTask();
        }else{
            displayTimetableLists();
        }
    }

    private void startBackgroundTask(){
        mProgress = ProgressDialog.show(this, "Loading tour packages",
                "Please wait...", true);
        mProgress.setCancelable(true);
        mProgress.setOnCancelListener(new OnCancelListener(){

            public void onCancel(DialogInterface args0){
                MondayActivity.this.finish();
            }
        });
        mProgress.show();
        mTask = new ClassesDownloadTask();
        mTask.execute();
    }

    private void loadTimetableLists(){

        try{
            Log.v("HIT", "loadTimetableLists triggered");
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 1000);

            DefaultHttpClient client = new DefaultHttpClient(params);
            HttpGet get = new HttpGet(LIST_URL);
            HttpResponse res = client.execute(get);
            int code = res.getStatusLine().getStatusCode();

            if (code != 200){
                throw new Exception("Invalid status code: " + code);
            }
            Log.v("XML", "Successfully fetched XML file");
            InputStream is = res.getEntity().getContent();

            timetableList = new ArrayList<HashMap<String, String>>();
            XmlPullParserFactory parseCreator = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parseCreator.newPullParser();
            parser.setInput(is, null);

            int event;
            String name = null;
            String time = null;
            String type = null;
            String room = null;
            String tag = null;
            while((event = parser.next()) != XmlPullParser.END_DOCUMENT){
                if(event == XmlPullParser.START_TAG){
                    tag = parser.getName();
                    if("class".equals(tag)){
                        name = time = type = room = "";
                    }
                } else if(event == XmlPullParser.TEXT){
                    if("time".equals(tag)){
                        time = parser.getText();
                    } else if("name".equals(tag)){
                        name = parser.getText();
                    }else if ("type".equals(tag)){
                        type = parser.getText();
                    }else if ("type".equals(tag)){
                        type = parser.getText();
                    }
                } else if (event == XmlPullParser.END_TAG){
                    if ("tour".equals(parser.getName())){
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("name", name);
                        map.put("time", time);
                        map.put("type", type);
                        map.put("room", room);
                        timetableList.add(map);
                        Log.v("XML", "loaded package: " + name);
                    }
                    tag = null;
                }
            }
        }catch (Exception e){
            Log.v("XML", "Error fetching monday.xml", e);
        }
    }

    private void displayTimetableLists(){
        Log.v("HIT", "displayTourPackages triggered");
        String from[] = {"name", "time", "type","room"};
        int to[] = {R.id.tName, R.id.tTime, R.id.typeTimetable,R.id.roomTimetable};
        SimpleAdapter a  = new SimpleAdapter(this, timetableList, R.layout.timetable_list, from, to);
        ListView l = (ListView) findViewById (R.id.courseList);
        l.setAdapter(a);
    }

    public class ClassesDownloadTask extends AsyncTask<Void,Void,Void>{
        boolean bFailed = false;
        @Override
        protected Void doInBackground(Void... params){
            Log.v("XML", "Loading data from AsyncTask");
            loadTimetableLists();
            if(!isCancelled()){
                mHandler.sendEmptyMessage(bFailed ? 1:0);
            }
            return null;
        }
    }
}
4

0 回答 0