简体   繁体   中英

Android Pull Parser from url.xml

I have searched for two days now, I probably have seen a solution that would solve my problem, though I have yet to find one I understand.

I am following this tutorial - Simple XMLPullParser Tutorial

The two areas I know I need to change are

Here 1

public List<Employee> parse(InputStream is) {
    XmlPullParserFactory factory = null;
    XmlPullParser parser = null;
    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newPullParser();

        parser.setInput(is, null);

        int eventType = parser.getEventType();

and Here 2

 List<Employee> employees = null;
    try {
        XMLPullParserHandler parser = new XMLPullParserHandler();
        employees = parser.parse(getAssets().open("employees.xml"));

This is from the working example, below is taken from what I am trying to achieve, removing this

employees = parser.parse(getAssets().open("employees.xml"));

I tried to get something like

URL url=new URL(" http://www.example.xml ");

URLConnection connection = url.openConnection();

InputStream in = connection.getInputStream();

employees = parser.parse(in);

The first piece I am not so sure what to change, the second I know I need to add the url, and open a connection. Then change the employees = parser... line, but to this point have no luck in getting a working response.

I have added android.permission.INTERNET.

I have also added my own XML file in the assets folder, a copy of the url I want to parse, which works fine.

Any help would be greatly appreciated.

Please see my answers here. Android : XmlpullParser exception

Also, make sure in

 XmlUtils.beginDocument(parser,"results");

"results" is your xml's first node name. In your case it might be "employees"

Also, for network related services may not be accessed through main UI thread.

Please, use AsyncTask and add you method in doInBackground() method.

 private class DownloadFilesTask extends AsyncTask<String, Void, String> {
 protected Long doInBackground(String... urls) {
   //put your xml parsing code here.
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM