android 抛出异常问题
我创建了一个android应用程序,我需要在其中使用登录。为此我必须解析 xml 以进行登录。我在登录按钮下编写以下代码。
loginButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
getInput();
parserMethod=new ParserMethod();
login=parserMethod.parseLoginStatus(userName,password,mobileNo,code);
if(login.getLoginStatus().equals("Sucess.."))
{
i=new Intent();
i.setClass(LoginActivity.this, MainActivity.class);
startActivity(i);
}
}
});
public Login parseLoginStatus(String userName, String password,String mobileNo, String code)
{
String sourceString="http://www.example.info/mobapp/Web_service/checkLogin.php?userId=robin&password=123456&mobile=0&code=8080&output=xml";
loadParseData(sourceString);
return MyXMLHandler.login;
}
private void loadParseData(String sourceString)
{
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl=new URL(sourceString);
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}
catch(Exception e)
{
System.out.println("XML Pasing Excpetion = " + e);
}
}
但问题是,当网络不可用或 xml 不可用时,应用程序就会崩溃。我该如何解决这个问题。提前致谢。
I have create an android application where i need to use login. For this i have to parse xml for login. i write the following code under login button.
loginButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
getInput();
parserMethod=new ParserMethod();
login=parserMethod.parseLoginStatus(userName,password,mobileNo,code);
if(login.getLoginStatus().equals("Sucess.."))
{
i=new Intent();
i.setClass(LoginActivity.this, MainActivity.class);
startActivity(i);
}
}
});
public Login parseLoginStatus(String userName, String password,String mobileNo, String code)
{
String sourceString="http://www.example.info/mobapp/Web_service/checkLogin.php?userId=robin&password=123456&mobile=0&code=8080&output=xml";
loadParseData(sourceString);
return MyXMLHandler.login;
}
private void loadParseData(String sourceString)
{
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl=new URL(sourceString);
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}
catch(Exception e)
{
System.out.println("XML Pasing Excpetion = " + e);
}
}
But problem is when the net is not available or the xml is not available then the application crashed . How can i solve the problem. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不应该在主 UI 线程中处理登录(在按钮
OnCLickListener
内)。请按照这篇文章查看如何使用AsyncTask.
First of all you should not process login in the main UI thread (inside the button
OnCLickListener
). Please follow this article to check how to useAsyncTask
.这应该可以解决你的问题...
this should solve your problem...