在我的Android 项目中,当我尝试连接到以下网页时, http://familytree-photoalbum.com/android.php ?a=login&email=saqib.amin1@gmail.com&password=JLjZAT13 并获取数据解析,..我的应用程序崩溃了,不知道我离开了什么。
这是解析文件的片段。
case R.id.bSignIn:
String e = email.getText().toString(); // Getting Email from LoginPage
String p = password.getText().toString(); // Getting Password from LoginPage
StringBuilder URL = new StringBuilder(baseURL);
URL.append(e + "&password=" + p);
String fullUrl = URL.toString();
new loadURL().execute(fullUrl);
break;
我正在使用的 AsyncTask。
protected class loadURL extends AsyncTask<String, Void, Intent> {
protected Intent doInBackground(String... fullUrl) {
return xmlParsing(fullUrl[0]);
}
protected void onPostExecute(Intent intent) {
startActivity(intent);
startActivity(new Intent(LoginPage.this, InvalidUser.class));
}
}
这是 xmlParsing 方法,从我的 AsyncTask 调用
protected Intent xmlParsing(String url) {
Intent activity = new Intent();
try {
URL loginPage = new URL(url);
// Getting XML Reader
/**
* The parse() method of SAXParser class reads the contents. The
* SAXParserFactory is a factory API that enables applications to
* configure and obtain a SAX parser to parse XML documents. The
* startElement() method retrieves all starting elements and prints
* them on the console. Whenever an error occurs it throws the
* SAXException.
*/
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingLoginPage doingWork = new HandlingLoginPage();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(loginPage.openStream()));
Log.i("info", doingWork.code());
if ((doingWork.userInformation()) == true) {
activity = new Intent(LoginPage.this, HomeScreen.class);
} else
activity = new Intent(LoginPage.this, InvalidUser.class);
} catch (Exception e1) {
e1.printStackTrace();
title.setText("Error");
}
return activity;
}
这是名为HandlingLoginPage.class的DefaultHandler 类
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Intent;
public class HandlingLoginPage extends DefaultHandler {
String defineCode = null;
boolean user = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("code")) {
defineCode = attributes.getValue("data");
if (defineCode.equals("IP")) {
user = false;
} else if (defineCode.equals("LS"))
user = true;
}
}
protected boolean userInformation (){
return user;
}
}