Android 应用程序不工作,不幸的是,XML已停止错误
我编写了以下代码来解析此xml: 但当我运行它时。。。它说应用程序停止工作,我无法找出原因。。。请帮忙 代码是: xmlActivity.java xmlParser.javaAndroid 应用程序不工作,不幸的是,XML已停止错误,android,eclipse,error-handling,Android,Eclipse,Error Handling,我编写了以下代码来解析此xml: 但当我运行它时。。。它说应用程序停止工作,我无法找出原因。。。请帮忙 代码是: xmlActivity.java xmlParser.java 不能在活动线程中执行像HttpRequest这样的慢速操作。您必须将它们放在单独的线程异步任务中。这是谷歌提到,但我失去了来源,我很抱歉 您应该尝试创建一个异步任务,下面是您应该如何尝试: public class GetSoigneurInfoTask extends AsyncTask<Document, In
不能在活动线程中执行像HttpRequest这样的慢速操作。您必须将它们放在单独的线程异步任务中。这是谷歌提到,但我失去了来源,我很抱歉 您应该尝试创建一个异步任务,下面是您应该如何尝试:
public class GetSoigneurInfoTask extends AsyncTask<Document, Integer, Document> //Le même code s'applique pour presque tout sauf onPostExecute()
{
Document doc;
String xml;
String url;
public GetSoigneurInfoTask(String URL)
{
url = URL;
}
protected Document doInBackground(Document...params)
{
xml = XmlFunctions.getXML(url);
doc = XmlFunctions.XMLfromString(xml);
return doc;
}
protected void onPostExecute(Document result)
{
if(result != null)
{
NodeList nodeList = doc.getElementsByTagName("RootTag"); //Crée une liste avec les elements sous soigneur
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
NodeList nodelist = element.getElementsByTagName("childTag");
Element element1 = (Element) nodelist.item(0);
NodeList fstNm = element1.getChildNodes();
soignTemp = fstNm.item(0).getNodeValue();
}
}
}
}
因此,您可以在doInBackground中调用您的方法,并在onPostExecute中完成所有与UI相关的操作
希望这有帮助 请为您的错误添加Logcat输出:异常堆栈跟踪等。我无法获取什么意思您不能在活动线程中执行类似HttpRequest的慢速操作我在另一个URL上执行此操作,它工作正常,仍在工作,但不在此URL上。当我尝试在浏览器中打开您的URL时,我在此处找到了它表示:缺少参数:xmin。是的,这是链接你真的能从中得到信息吗?XML文件应该如下所示。我的意思是他们展示了很多例子,但我相信这些都是不可解析的。如果我错了,请纠正我!
package com.example.xml;
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_latitude = "latitude";
static final String KEY_longitude = "longitude";
static final String KEY_location = "location ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String latitude = in.getStringExtra(KEY_latitude);
String longitude = in.getStringExtra(KEY_longitude);
String location = in.getStringExtra(KEY_location);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.cost_label);
TextView lblDesc = (TextView) findViewById(R.id.description_label);
lblName.setText(latitude);
lblCost.setText(longitude);
lblDesc.setText(location);
}
}
package com.example.xml;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
*
* @param url
* string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
*
* @param XML
* string
* */
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/**
* Getting node value
*
* @param elem
* element
*/
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
*
* @param Element
* node
* @param key
* string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
public class GetSoigneurInfoTask extends AsyncTask<Document, Integer, Document> //Le même code s'applique pour presque tout sauf onPostExecute()
{
Document doc;
String xml;
String url;
public GetSoigneurInfoTask(String URL)
{
url = URL;
}
protected Document doInBackground(Document...params)
{
xml = XmlFunctions.getXML(url);
doc = XmlFunctions.XMLfromString(xml);
return doc;
}
protected void onPostExecute(Document result)
{
if(result != null)
{
NodeList nodeList = doc.getElementsByTagName("RootTag"); //Crée une liste avec les elements sous soigneur
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
NodeList nodelist = element.getElementsByTagName("childTag");
Element element1 = (Element) nodelist.item(0);
NodeList fstNm = element1.getChildNodes();
soignTemp = fstNm.item(0).getNodeValue();
}
}
}
}