简体   繁体   中英

Application not responding

I am sending details to be stored in a database using a web service using KSOAP.This code was working perfectly before and I didn't make any change to it. Now its not working. Please help. I checked the web service and it works. I have attached the log cat details. 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:

                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'){
                        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
                    }
                }
                catch(Exception e)
                {
                   e.printStackTrace();
                }

            break;
        }
           }
        });
    }

}

在此处输入图片说明

Pretty simple reason why you're getting an Application Not Responding error: you are making the web request on the main (UI) thread. There are two rules to the Android threading model: 1) don't block the main thread for more than a couple seconds 2) don't update the UI from off of the main thread. You are violating the first of those rules. You should look into using AsyncTask to do your longer operations.

httpTransport.call(SOAP_ACTION, envelope); You are making a network call on the UI thread. You need to do all long running tasks, such as accessing a resource over the network on a separate thread. The API provides a convenience class call [AsyncTask][1] that lets you accomplish this easily.

In its simplest form, you would create a class that extends AsyncTask, and then move your httpTransport.call(SOAP_ACTION, envelope); to its doInBackground method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM