Search code examples
androidweb-servicessoapandroid-ksoap2

Invalid user login details error message in Android


I'm trying to make a message appears when the user enters the wrong username/password.

My code is working when the user enters the correct login details (it goes to the dashboard) But I want an error message to appear saying "Invalid username/password" and then and "Ok" button, once the user click on the ok button, the login page get refreshed so that the user can enter the login details again.

Please help me!!

Thats my login.java

public class Login extends ActionBarActivity implements OnClickListener  {

    private static final String NAMESPACE = "***"; 
    private static final String URL = "***";
    private static final String METHOD_NAME = "login";
    private static final String SOAP_ACTION =  NAMESPACE + "/" + METHOD_NAME; //in wsdl it's nothing

    EditText usersusername, userspassword;
    Button LB;



    @Override 


    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login); 
         LB = (Button) findViewById(R.id.loginbutton);
         LB.setOnClickListener(this);
         usersusername = (EditText)findViewById(R.id.editusername);
         userspassword = (EditText)findViewById(R.id.editpassword);




    }

    public void onClick(View view){
        if(usersusername.getText().length()==0) {
            usersusername.setError("Please enter your username");
        }
        if(userspassword.getText().length()==0) {
            userspassword.setError("Please enter your password");
        }
    switch (view.getId()){

    case R.id.loginbutton:
    new LongOperation().execute("");

    break;


    }
    }


    private class LongOperation extends AsyncTask<String, Void, String>{ 

        @Override
        protected String doInBackground(String... params) {

           SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           usersusername = (EditText)findViewById(R.id.editusername);
           userspassword = (EditText)findViewById(R.id.editpassword);
           String user_Name = usersusername.getText().toString();
           String user_Password = userspassword.getText().toString();


           PropertyInfo unameProp =new PropertyInfo();
           unameProp.setName("userName");//Define the variable name in the web service method
           unameProp.setValue(user_Name);//set value for userName variable
           unameProp.setType(String.class);//Define the type of the variable
           request.addProperty("username",user_Name);//Pass properties to the variable
           //Using this to add parameters "username" grabbed from WSDL

           PropertyInfo passwordProp =new PropertyInfo();
           passwordProp.setName("password");
           passwordProp.setValue(user_Password);
           passwordProp.setType(String.class);
           request.addProperty(passwordProp);




           SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // Declare the version of the soap request
           envelope.setOutputSoapObject(request);

           try {
                HttpTransportSE aht = new HttpTransportSE(URL);     
                aht.call(SOAP_ACTION, envelope); 
                SoapPrimitive result =(SoapPrimitive)envelope.getResponse(); // the result will be a session ID
                String sessionid = result.toString(); // convert the session ID to a string so we can check if we got something or not
                //System.out.println( Something);
                if (sessionid != null ) 
                {
                   Intent intent = new Intent(Login.this,Dashboard.class);
                   intent.putExtra("username",usersusername.getText().toString());
                   startActivity(intent);
                }
                else 
                {      
                  Intent intent = new Intent(Login.this,Login.class);
                   startActivity(intent);
           }
           }
           catch (Exception e){
            e.printStackTrace();           }

            return null; 

        } // closing bracket of do in background
    } // closing bracket of long operation

}

and thats the error i get when i enter the WRONG login details

01-25 20:24:41.983: W/System.err(9311): SoapFault - faultcode: 'soapenv:Client' faultstring: 'Error - invalid login name' faultactor: '' detail: org.kxml2.kdom.Node@4248fda8
01-25 20:24:41.983: W/System.err(9311):     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:147)
01-25 20:24:41.993: W/System.err(9311):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
01-25 20:24:41.993: W/System.err(9311):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
01-25 20:24:41.993: W/System.err(9311):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)
01-25 20:24:41.993: W/System.err(9311):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
01-25 20:24:41.993: W/System.err(9311):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
01-25 20:24:41.993: W/System.err(9311):     at com.datacom.caservicedesk.Login$LongOperation.doInBackground(Login.java:120)
01-25 20:24:41.993: W/System.err(9311):     at com.datacom.caservicedesk.Login$LongOperation.doInBackground(Login.java:1)
01-25 20:24:41.993: W/System.err(9311):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-25 20:24:41.993: W/System.err(9311):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-25 20:24:41.993: W/System.err(9311):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-25 20:24:41.993: W/System.err(9311):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-25 20:24:41.993: W/System.err(9311):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

Solution

  • AsyncTask has also an onPostExecute callback which is called when doInBackground completes. So you should handle the result here and update the UI accordingly.

    private class LongOperation extends AsyncTask<String, Void, String>{ 
    
            @Override
            protected void onPostExecute(String param) {
                if(param == null) {
                    // show dialog and prepare the fields for retry 
                }
            }
    }