gpt4 book ai didi

Android XMLparse 到 Arraylist

转载 作者:行者123 更新时间:2023-11-30 04:04:39 27 4
gpt4 key购买 nike

抱歉,如果我让事情变得困惑。我编辑了整个问题。

我正在为托管在我的网络服务器上的网址使用 XMLPARSER。

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

java.lang.ArrayStoreException: source[0] of type java.util.HashMap cannot be stored in destination array of type 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);
}

}

最佳答案

试试这段代码

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);
}
}

关于Android XMLparse 到 Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11928918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com