android xmppullpaser从url读取xml

时间:2016-05-24 09:17:06

标签: android xmlpullparser

我有xml是list_img.xml

<employees>
<emp>
    <name>jame</name>
    <gender>male</gender>
    <age>36</age>
</emp>
<emp>
    <name>natalie</name>
    <gender>female</gender>
    <age>28</age>
</emp>
<emp>
    <name>phil</name>
    <gender>male</gender>
    <age>30</age>
</emp>
<emp>
    <name>frank</name>
    <gender>male</gender>
    <age>33</age>
</emp>

这是我读取list_img.xml的代码,如果它在资产文件夹

public class FragmentImg extends Fragment {
static final String KEY_EMP = "emp";
static final String KEY_NAME = "name";
static final String KEY_GENDER = "gender";
static final String KEY_AGE = "age";
List<HashMap<String,String>> imgHashmap;
List<ClassImg> imgList = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_img, container, false);
    ImgActivity activity = (ImgActivity) getActivity();
    String uri = "list_img.xml";
    GridView gv_img = (GridView)view.findViewById(R.id.gridViewImg);
    try {
        XmlPullParserImg parser_Img = new XmlPullParserImg();
        imgList = parser_Img.parse(getActivity().getAssets().open(uri));
        BinderDataImg bd_img = new BinderDataImg(getActivity(), imgHashmap);
        gv_img.setAdapter(bd_img);
        gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent i = new Intent();
                i.setClass(getActivity(), ImgDetail.class);
                startActivity(i);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return view;
}
public class XmlPullParserImg {
    private ClassImg c_i;
    private String text;
    public XmlPullParserImg() {
        danhsachList = new ArrayList<ClassImg>();        }
    public List<ClassImg> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            imgHashmap = new ArrayList<HashMap<String,String>>();
            HashMap<String,String> map = null;
            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("KEY_EMP")) {
                            c_i = new ClassImg();
                            map = new HashMap<String,String>();
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("KEY_EMP")) {
                            imgList.add(c_ds);
                            imgHashmap.add(map);
                        } else if (tagname.equalsIgnoreCase(KEY_NAME)) {
                            c_i.setName(text);
                            map.put(KEY_NAME, text);
                        } else if (tagname.equalsIgnoreCase(KEY_GENDER)) {
                            c_i.setGender(text);
                            map.put(KEY_GENDER, text);
                        } else if (tagname.equalsIgnoreCase(KEY_AGE)) {
                            c_i.setAge(text);
                            map.put(KEY_AGE, text);
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgList;
    }
}

}

所以,一切都好。然后我将list_img.xml上传到dropbox
这是链接https://www.dropbox.com/s/lh2ucpbvpqloa3e/list_img.xml?dl=0
我改变了代码

public class FragmentImg extends Fragment {
static final String KEY_EMP = "emp";
static final String KEY_NAME = "name";
static final String KEY_GENDER = "gender";
static final String KEY_AGE = "age";
List<HashMap<String,String>> imgHashmap;
List<ClassImg> imgList = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_img, container, false);
    ImgActivity activity = (ImgActivity) getActivity();
    String uri = "https://www.dropbox.com/s/lh2ucpbvpqloa3e/list_img.xml?dl=0";
    GridView gv_img = (GridView)view.findViewById(R.id.gridViewImg);
    try {
        XmlPullParserImg parser_Img = new XmlPullParserImg();
        imgList = parser_Img.parse(getActivity().getAssets().open(uri));
        BinderDataImg bd_img = new BinderDataImg(getActivity(), imgHashmap);
        gv_img.setAdapter(bd_img);
        gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent i = new Intent();
                i.setClass(getActivity(), ImgDetail.class);
                startActivity(i);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return view;
}
public class XmlPullParserImg {
    private ClassImg c_i;
    private String text;
    public XmlPullParserImg() {
        danhsachList = new ArrayList<ClassImg>();        }
    public List<ClassImg> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            imgHashmap = new ArrayList<HashMap<String,String>>();
            HashMap<String,String> map = null;
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            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("KEY_EMP")) {
                            c_i = new ClassImg();
                            map = new HashMap<String,String>();
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("KEY_EMP")) {
                            imgList.add(c_ds);
                            imgHashmap.add(map);
                        } else if (tagname.equalsIgnoreCase(KEY_NAME)) {
                            c_i.setName(text);
                            map.put(KEY_NAME, text);
                        } else if (tagname.equalsIgnoreCase(KEY_GENDER)) {
                            c_i.setGender(text);
                            map.put(KEY_GENDER, text);
                        } else if (tagname.equalsIgnoreCase(KEY_AGE)) {
                            c_i.setAge(text);
                            map.put(KEY_AGE, text);
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgList;
    }
}
}

我无法从url读取list_img.xml 为什么 ?怎么解决? 谢谢你的阅读。

1 个答案:

答案 0 :(得分:0)

getActivity().getAssets().open(uri)是问题:您的外部URI不是可以在应用程序本身的资产中找到的东西。

您需要使用HTTP请求API来获取所提供的URI的InputStream,例如OkHttp,UrlConnection,Volley等。