我的应用程序LogCat在调试模式下运行时给出了这个输出:
我正在尝试解析从HTTPGet请求收到的XML文档(String result)结果是字符串格式的XML。只是不能把它放到字符串值来测试登录。
02-20 19:39:48.326:E / AndroidRuntime(774):致命异常:主02-20 19:39:48.326:E / AndroidRuntime(774):java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl无法强制转换为 android.sax.Element 02-20 19:39:48.326:E / AndroidRuntime(774):at com.testingapp.MainActivity $ startLogin.onPostExecute(MainActivity.java:112) 02-20 19:39:48.326:E / AndroidRuntime(774):at com.testingapp.MainActivity $ startLogin.onPostExecute(MainActivity.java:1) 02-20 19:39:48.326:E / AndroidRuntime(774):at android.os.AsyncTask.finish(AsyncTask.java:631)02-20 19:39:48.326: E / AndroidRuntime(774):at android.os.AsyncTask.access $ 600(AsyncTask.java:177)02-20 19:39:48.326:E / AndroidRuntime(774):at android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 02-20 19:39:48.326:E / AndroidRuntime(774):at android.os.Handler.dispatchMessage(Handler.java:99)02-20 19:39:48.326:E / AndroidRuntime(774):at android.os.Looper.loop(Looper.java:137)02-20 19:39:48.326: E / AndroidRuntime(774):at android.app.ActivityThread.main(ActivityThread.java:5039)02-20 19:39:48.326:E / AndroidRuntime(774):at java.lang.reflect.Method.invokeNative(Native Method)02-20 19:39:48.326:E / AndroidRuntime(774):at java.lang.reflect.Method.invoke(Method.java:511)02-20 19:39:48.326: E / AndroidRuntime(774):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 02-20 19:39:48.326:E / AndroidRuntime(774):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)02-20 19:39:48.326:E / AndroidRuntime(774):at dalvik.system.NativeStart.main(原生方法)
我能够将错误集中到我的onPostExecute AsyncTask:
@Override
protected void onPostExecute(String result) {
int z = 0;
String xml = result;
String KEY_ITEM = "Response";
String KEY_SUCCESS = "Success";
String KEY_FAIL = "Fail";
String KEY_UID = "UID";
Document doc = getDomElement(xml);
if(xml != null)
{
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++)
{
Element e = (Element) nl.item(i);
String successString = getValue(e,KEY_SUCCESS);
String UIDString = getValue(e,KEY_UID);
String failString = getValue(e,KEY_FAIL);
if(failString.equals("Fail")){
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(MainActivity.this, "Please check username and password.", duration);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
toast.show();
}
else{
if(successString.equals("Success")){
Intent intent = new Intent (MainActivity.this, HomeActivity.class);
startActivity(intent);
}
}
}
}
else
{
Toast toast = Toast.makeText(MainActivity.this, "No Connection to Retrieve Data.", Toast.LENGTH_LONG);
toast.show();
}
}
我不明白我做错了什么?
答案 0 :(得分:2)
这是问题...... 改变这个
public String getValue(Element item, String str) {
NodeList n = ((Document) item).getElementsByTagName(str);
return getElementValue(n.item(0));
}
到
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str); // Removed Typecast
return getElementValue(n.item(0));
}
您正在转换Element.getElementsByTagName(str)正在返回一个NodeList对象。 但是你将它转换为Document对象,因此是类转换异常。