gpt4 book ai didi

java - 在 android 中读取 XML 响应

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:33 24 4
gpt4 key购买 nike

我正在尝试从以下网址读取 XML 响应 http://sagt.vizushop.com/DefaultSimple.aspx?command=default

我在我的 android 项目中使用以下代码获取第一个项目,然后获取其第二个节点的值。

String uri="http://sagt.vizushop.com/DefaultSimple.aspx?command=default";
String [] data=new String [9];
try{
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);

con.connect();
data = new String[9];
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(con.getInputStream());
NodeList itemnodes=doc.getElementsByTagName("item");
Node firstitemnode=itemnodes.item(0);
NodeList childnodesoffirstitemnode=firstitemnode.getChildNodes();
Node firstnodeOffirstitem=childnodesoffirstitemnode.item(1);
String contenturl=firstnodeOffirstitem.getNodeValue().toString();
data[0] = contenturl;
}

但它给出了 java.lang.NullPointerException。

请帮忙解决这个问题

提前致谢

最佳答案

请使用以下代码使用 Dom Parser 进行 XML 解析。

public class XMLParsingDOMExample extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);

/** Create a new textview array to display the results */
TextView id[];
TextView imageurl[];

try {

URL url = new URL("http://sagt.vizushop.com/DefaultSimple.aspx?command=default");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName("item");

/** Assign textview array lenght by arraylist size */
id = new TextView[nodeList.getLength()];
imageurl = new TextView[nodeList.getLength()];

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

Node node = nodeList.item(i);

id[i] = new TextView(this);
imageurl[i] = new TextView(this);

Element fstElmnt = (Element) node;
NodeList idList = fstElmnt.getElementsByTagName("item_id");
Element idElement = (Element) idList.item(0);
idList = idElement.getChildNodes();
id[i].setText("id is = " + ((Node) idList.item(0)).getNodeValue());

NodeList imageurlList = fstElmnt.getElementsByTagName("item_image");
Element imageurlElement = (Element) imageurlList.item(0);
imageurlList = imageurlElement.getChildNodes();
imageurl[i].setText("imageurl is = " + ((Node) imageurlList.item(0)).getNodeValue());

layout.addView(id[i]);
layout.addView(imageurl[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}

/** Set the layout view to display */
setContentView(layout);
}
}

并查看下面的链接以获取更多信息。

Dom Parsing Example

关于java - 在 android 中读取 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12313488/

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