1

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.

dulal_026
  • 589
  • 1
  • 6
  • 20

2 Answers2

0

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 use AsyncTask.

denis.solonenko
  • 11,645
  • 2
  • 28
  • 23
0

this should solve your problem...

 public Login parseLoginStatus(String userName, String password,String mobileNo, String code) 
    {
        String sourceString="http://www.amarhost.info/mobapp/Web_service/checkLogin.php?userId=databiz&password=123456&mobile=0&code=8080&output=xml";

    if(sourceString.equals("message it gives when there is no net connection")){
    return null;
    }
        //String sourceString="http://www.amarhost.info/mobapp/Web_service/checkLogin.php?userId="+userName+"&password="+password+"&mobile="+mobileNo+"&code="+code+"&output=xml";
        loadParseData(sourceString);
        return MyXMLHandler.login;
    }
ngesh
  • 13,398
  • 4
  • 44
  • 60