如何从url而不是getAssets获取xml文件

时间:2015-05-11 21:59:03

标签: java android xml

我正在尝试对我的应用进行此更改,但是我失败了,database.xml在资产中......应用程序显示此xml,此xml文件存在于资产中。我如何从URL获取此文件?

MainActivity.java:

package com.Hixa.streattraffic;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity
{
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        List<Street> streets = null;
        try
        {
            ParserHandler parser = new ParserHandler();
            streets = parser.parse(getAssets().open("database.xml"));
            ArrayAdapter<Street> adapter = new ArrayAdapter<Street>(this,     R.layout.list_item, streets);
            listView.setAdapter(adapter);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

ParserHandler.java

package com.Hixa.streattraffic;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class ParserHandler
{

    List<Street> streets;
    private Street street;
    private String text;

    public ParserHandler()
    {
        streets = new ArrayList<Street>();
    }

    public List<Street> getStreets()
    {
        return streets;
    }

    public List<Street> 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();
            while(eventType != XmlPullParser.END_DOCUMENT)
            {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("street"))
                    {
                        // create a new instance of street
                        street = new Street();
                    }
                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if(tagname.equalsIgnoreCase("street"))
                    {
                        // add street object to list
                        streets.add(street);
                    }
                    else if(tagname.equalsIgnoreCase("Time"))
                    {
                        street.setTime(text);
                    } 
                    /*else if (tagname.equalsIgnoreCase("id")) {
                        street.setId(Integer.parseInt(text));
                    }*/
                    else if(tagname.equalsIgnoreCase("Street_Name"))
                    {
                        street.setStreet_Name(text);
                    }
                    else if(tagname.equalsIgnoreCase("Intersection_No."))
                    {
                        street.setIntersection_No(text);
                    }
                    else if (tagname.equalsIgnoreCase("Traffic_State"))
                    {
                        street.setTraffic_State(text);
                    } 
                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return streets;
    }
}

street.java:

package com.Hixa.streattraffic;

public class Street {
    //private int id;
    private String Street_Name;
    private String Intersection_No;
    private String Traffic_State;
    private String Time;

    public Street(){}

    /*public Street(int id)
    {
        this.id = id;
    }*/

        public Street(String Street_Name, String Intersection_No, String Traffic_State, String Time)
    {
        //this.id = id;
        this.Street_Name = Street_Name;
        this.Intersection_No = Intersection_No;
        this.Traffic_State = Traffic_State;
        this.Time = Time;
    }

    public String getStreet_Name()
    {
        return this.Street_Name;
    }

    public void setStreet_Name(String Street_Name)
    {
        this.Street_Name = Street_Name;
    }

    public String getIntersection_No()
    {
        return this.Intersection_No;
    }

    public void setIntersection_No(String Intersection_No)
    {
        this.Intersection_No = Intersection_No;
    }

    public String getTraffic_State()
    {
        return this.Traffic_State;
    }

    public void setTraffic_State(String Traffic_State)
    {
        this.Traffic_State = Traffic_State;
    }

    public String getTime()
    {
        return this.Time;
    }

    public void setTime(String Time)
    {
        this.Time = Time;
    }

    @Override
    public String toString()
    {
        return "Time : " + Time + "\nStreet Name : " + Street_Name +"\nStreat     Intersection No. : " + Intersection_No+ "\nStreat Traffic State : " + Traffic_State;
    }
}

我的问题是(希望你能帮忙)在MainActivity.java中有这个功能

streets = parser.parse(getAssets().open("database.xml"));

我想从服务器获取相同的XML文件,我该如何实现?

感谢

1 个答案:

答案 0 :(得分:0)

试试这个

InputStream stream = null;
    URL RssUrl = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) RssUrl.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    stream = conn.getInputStream(); 

然后在你的解析方法中使用这个流