1

对不起,如果我让事情变得混乱。我编辑了整个问题。

我将 XMLPARSER 用于托管在我的网络服务器上的网址。

我想解析我的 url 列表,以便我可以从我的 XMLparser 中检索 url url.add(map);intent.putExtra(Extra.IMAGES, imageUrls);请指导我如何使用代码进行操作。我的尝试显示错误

java.lang.ArrayStoreException:java.util.HashMap 类型的源 [0] 无法存储在 java.lang.String [] 类型的目标数组中

public class HomeActivity extends BaseActivity {

        static final String URL = "http://*******";


        static final String KEY_LIST = "list";
        static final String KEY_THUMB_URL = "thumb_url";



        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ac_home);

            ArrayList<HashMap<String, String>> url = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL);
            Document doc = parser.getDomElement(xml); 
            NodeList nl = doc.getElementsByTagName(KEY_LIST);

            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value

                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                url.add(map); // <***** Set the results from here to imageUrls
            }

        imageUrls = (String[]) url.toArray(new String[0]); // <----***Error****

        }
        public void onImageGridClick(View view) {
            Intent intent = new Intent(this, ImageGridActivity.class);
            intent.putExtra(Extra.IMAGES, imageUrls); 
            startActivity(intent);
        }

        public void onImagePagerClick(View view) {
            Intent intent = new Intent(this, ImagePagerActivity.class);
            intent.putExtra(Extra.IMAGES, imageUrls);
            startActivity(intent);
        }

    }
4

1 回答 1

1

试试这个代码

public class MainActivity extends BaseActivity {

    String[] imageUrls;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_home);
        ArrayList<String> url = new ArrayList<String>();
        try {
            URL url_link = new URL("http:******");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url_link.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("list");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                Element fstElmnt = (Element)node;
                NodeList nameList = fstElmnt.getElementsByTagName("thumb_url");
                Element nameElement = (Element)nameList.item(0);
                nameList = nameElement.getChildNodes();

                url.add(nameList.item(0).getNodeValue());
            }
            imageUrls = (String[]) url.toArray(new String[0]);
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

    }
    public void onImageGridClick(View view) {
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(Extra.IMAGES, imageUrls); 
        startActivity(intent);
    }

    public void onImagePagerClick(View view) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(Extra.IMAGES, imageUrls);
        startActivity(intent);
    }
}
于 2012-08-13T08:16:14.387 回答