0

I am sending details to be stored in a database using a web service using KSOAP.There is nothing wrong with the web service it works fine. This code worked when i didn't user AsyncTask class. I am new to android and this is the first time i tried using the AsyncTask class and it does not work. I have attached the log cat errors, something is wrong with the doinbackround method. What am I doing wrong? Please help

public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
Button sqlRegister, sqlView;

EditText  sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);

sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);

sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        switch (v.getId()){
        case R.id.bRegister:
        new LongOperation().execute("");
        break;
      }
     }
    });
}

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

    @Override
    protected String doInBackground(String... params) {
         String firstname = sqlFirstName.getText().toString();
         String lastname = sqlLastName.getText().toString();
         String emailadd = sqlEmail.getText().toString();
         String number = sqlMobileNumber.getText().toString();
         String loc = sqlCurrentLocation.getText().toString();
         String uname = sqlUsername.getText().toString();
         String pwd = sqlPassword.getText().toString();

         SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
         Request.addProperty("fname", String.valueOf(firstname));
         Request.addProperty("lname", String.valueOf(lastname));
         Request.addProperty("email", String.valueOf(emailadd));
         Request.addProperty("num", String.valueOf(number));
         Request.addProperty("loc", String.valueOf(loc));
         Request.addProperty("username", String.valueOf(uname));
         Request.addProperty("password", String.valueOf(pwd));
         Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(Request);
         HttpTransportSE httpTransport  = new HttpTransportSE(SOAP_ADDRESS);
         try
         {
            httpTransport.call(SOAP_ACTION, envelope);
             SoapObject response = (SoapObject)envelope.getResponse();
             int result =  Integer.parseInt(response.getProperty(0).toString());
             if(result == '1')
             {
                 return "Registered";
             }
             else
             {
                 return "Not Registered";
             }
         }catch(Exception e){
            e.printStackTrace();
        }
         return null;

    }      

    @Override
    protected void onPostExecute(String result) {
        if(result=="Registered")
        {
        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
        }
        else if(result =="Not Registered")
        {
        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
    }   
 }

enter image description here

////Edited

public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
Button sqlRegister, sqlView;

EditText  sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);

sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);

sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        switch (v.getId()){
        case R.id.bRegister:
        new LongOperation().execute("");
        break;
      }
     }
    });
}

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

    @Override
    protected String doInBackground(String... params) {
         String firstname = sqlFirstName.getText().toString();
         String lastname = sqlLastName.getText().toString();
         String emailadd = sqlEmail.getText().toString();
         String number = sqlMobileNumber.getText().toString();
         String loc = sqlCurrentLocation.getText().toString();
         String uname = sqlUsername.getText().toString();
         String pwd = sqlPassword.getText().toString();

         SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
         Request.addProperty("fname", String.valueOf(firstname));
         Request.addProperty("lname", String.valueOf(lastname));
         Request.addProperty("email", String.valueOf(emailadd));
         Request.addProperty("num", String.valueOf(number));
         Request.addProperty("loc", String.valueOf(loc));
         Request.addProperty("username", String.valueOf(uname));
         Request.addProperty("password", String.valueOf(pwd));

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(Request);
         HttpTransportSE httpTransport  = new HttpTransportSE(SOAP_ADDRESS);
         Log.d("work","work");
         try
         {
            httpTransport.call(SOAP_ACTION, envelope);
             SoapObject response = (SoapObject)envelope.getResponse();
             int result =  Integer.parseInt(response.getProperty(0).toString());
             if(result == 1)
             {
                 Log.d("reg","reg");
                 return "Registered";
             }
             else
             {
                 Log.d("no","no");
                 return "Not Registered";
             }
         }catch(Exception e){
            e.printStackTrace();
        }
         return null;

    }      

    @Override
    protected void onPostExecute(String result) {
        Log.d("tag","onpost");
        if(result!=null)
        {

            if(result.equals("Registered"))
                {
                    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
                }
            else if(result.equals("Not Registered"))
                {
                    Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
                }
        }
        else
        {
            Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    }   

}

Sindu_
  • 1,347
  • 8
  • 27
  • 67

4 Answers4

0

Try this....

  1. Always keep the UI work in UI thread, and Non-UI work in Non-UI thread.

  2. doInBackground is a Non-UI thread, so you can NOT post anything on the UI thread from this.

  3. The Toast statement is the problem in doInBackgroud, move it to postExecute, which is working on UI thread.

    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
    
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

You're calling

Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

inside doInBackground(), which not running in UI thread. That causing the problem.

Try this:

runOnUiThread(new Runnable() {
    public void run() {
         Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
    }
});
}

or move the Toast.makeText to onPostExecute() method, which running in UI thread

user1417127
  • 1,525
  • 1
  • 21
  • 29
  • I removed the Toast from the doInBackground(). I don't get any errors but nothing happens when i click on the Register button. None of the toasts are displayed :( – Sindu_ Jul 11 '12 at 04:50
  • How can the Toast displayed if you removed it? And you don't have to remove it. Try the code I post above. Run the `Toast.makeText` inside runOnUiThread. It should works. – user1417127 Jul 11 '12 at 05:34
0

This is because you are calling Toast in the doInBackground(). Whatever UI in doInBackground will make the looper() exception. so try to make the UI in the PostExecute() in AsynTask as

@Override
protected void onPostExecute(String result) {

 Toast.makeText(Registration.this, "You have been registered Successfully",Toast.LENGTH_LONG).show();

}
 }

It may helpful to you..

deepa
  • 2,496
  • 1
  • 26
  • 43
0

you should not compare strings using ==.

protected void onPostExecute(String result) {
        if(result=="Registered")

That could be the reason behind the unexpected behavior. Try using something like

result.equals("registered")

read this

Vinay W
  • 9,912
  • 8
  • 41
  • 47