Search code examples
androidweb-servicesandroid-asynctaskandroid-ksoap2

Soap webservices in android login


I have an one android application. Here I have to create the login page with mysql connection using soap webservices.

My code is working well on android 2.2 version.but its not working in android 4.0.

So here i have to use asynchronousTask .But i didn't know the asynchronousTask.please help me to resolve these problems.

EDIT:

i have using following code:

public class AndroidLoginExampleActivity extends Activity {
private final String NAMESPACE = "http://ws.userlogin.com";
private final String URL = "http://111.223.128.10:8085/AndroidLogin/services/Login?wsdl";
private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
private final String METHOD_NAME = "authentication";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button login = (Button) findViewById(R.id.btn_login);
    login.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
new LongOperation().execute();

  }
    } );
  }
 class LongOperation extends AsyncTask<String, Void, String> {
private ProgressDialog Dialog = new ProgressDialog(LoginActivity.this);

@Override
protected String doInBackground(String... aurl) {
// TODO Auto-generated method stub
loginAction();
return null;
   }

    protected void onPreExecute() {
    Dialog.setMessage("Loading...");
    Dialog.show();
     }

      protected void onPostExecute(String resultGot) {
    Dialog.dismiss();
     }
    }
    private void loginAction(){
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    EditText userName = (EditText) findViewById(R.id.tf_userName);
    String user_Name = userName.getText().toString();
    EditText userPassword = (EditText) findViewById(R.id.tf_password);
    String user_Password = userPassword.getText().toString();

  //Pass value for userName variable of the web service
    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(unameProp);//Pass properties to the variable

  //Pass value for Password variable of the web service
    PropertyInfo passwordProp =new PropertyInfo();
    passwordProp.setName("password");
    passwordProp.setValue(user_Password);
    passwordProp.setType(String.class);
    request.addProperty(passwordProp);

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    androidHttpTransport = new HttpTransportSE(URL);

    runOnUiThread(new Runnable() {
            @Override
            public void run() {
        try{

         androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

           TextView result = (TextView) findViewById(R.id.tv_status);
           result.setText(response.toString());

    }
    catch(Exception e){

    }
      });
   }

   }

now also am getting the same error:

Before that:

Now i have to create the application on target version android 4.0 means its working well on android 2.2.but it didn't work on android 4.0 version.please check my code and give me solution .


Solution

  • Step 1. Please make a class that extends AsyncTask in your existing AndroidLoginExampleActivity like below :

    class LongOperation extends AsyncTask<String, Void, String> {
            private ProgressDialog Dialog = new ProgressDialog(OffersActivity.this);
    
        @Override
        protected String doInBackground(String... aurl) {
            // TODO Auto-generated method stub
            loginAction();
            return null;
        }
    
        protected void onPreExecute() {
                Dialog.setMessage("Loading...");
                Dialog.show();
        }
    
        protected void onPostExecute(String resultGot) {
                Dialog.dismiss();
        }
     }
    

    step 2. Make a call of your method loginAction(); in which you have implemented your server implementation code.

    Step 3. Execute LongOperation class from your activity's AndroidLoginExampleActivity onCreate() method like below :

    public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    new LongOperation().execute();
    
    }
    

    Step 4. Just you want to know that you cant make any UI task in directly in doInBackground() in your case you are implementing text view here, for this issue you have to make runOnUiThread(new Runnable() and then make code of all UI (in your case TextView)..See below code...

     runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
    
        try{
                androidHttpTransport.call(SOAP_ACTION, envelope);
                   SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
    
                   TextView result = (TextView) findViewById(R.id.tv_status);
                   result.setText(response.toString());
    
            }
            catch(Exception e){
    
            }
    
        }
             });
    

    you can directly paste this code portion inside your loginAction() method....

    Let me know if any confusion...

    Hope, now you can run your app in ICS and JellyBean too... :)