2011-08-04 13 views
1

私はログインを使用する必要があるアンドロイドアプリケーションを作成しました。これは、私はログインのために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が利用できず、アプリケーションがクラッシュした場合です。どうすれば問題を解決できますか?前もって感謝します。

答えて

0

最初に、メインのUIスレッド(ボタンOnCLickListener内)でログインを処理しないでください。 AsyncTaskの使用方法を確認するには、this articleに従ってください。これはあなたの問題を解決する必要があり

0

...

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; 
    } 
関連する問題