Monday, 13 April 2015

Making Soap Web Service call in Android

Before Starting we need to take care of these for parameters

1. NAMESPACE
2. SOAP_ACTION
3. URL
4. METHOD_NAME

Fill these values correctly.You can get these values by hitting your url in browser as shown in below image :





SOAP_ACTION = NAMESPACE+MTHOD_NAME (always)

URL will be your web service url. 

Now the java code in android to get the result from the soap service:

1. MainActivity.java

public class MainActivity extends ActionBarActivity {



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 new BackTask().execute();

}



class BackTask extends AsyncTask<Void, Void, Void>{



@Override

protected Void doInBackground(Void... params) {

WebserviceCall com = new WebserviceCall();

          //Call Webservice class method and pass values and get response

          String mResponse = com.getSoapResponse("Login");  

          Log.i("Output", "----"+mResponse);

return null;

}

}



}




2. WebserviceCall.java

public class  WebserviceCall {

/**

 * Variable Declaration................

 *

 */

String namespace = "http://tempuri.org/";

private String url = "your URL Here";

String SOAP_ACTION;

SoapObject request = null, objMessages = null;

SoapSerializationEnvelope envelope;

AndroidHttpTransport androidHttpTransport;


public String getSoapResponse(String MethodName)

{

try {
SOAP_ACTION = namespace + MethodName;

//Adding values to request object

request = new SoapObject(namespace, MethodName);


//Adding String value to request object (User name in this case)

PropertyInfo mUsername =new PropertyInfo();

mUsername.setName("UserName");

mUsername.setValue("kamal@gmail.Com");

mUsername.setType(String.class);

request.addProperty(mUsername);


//Adding String value to request object (password in this case)

PropertyInfo mPassword =new PropertyInfo();

mPassword.setName("Password");

mPassword.setValue("122");

mPassword.setType(String.class);

request.addProperty(mPassword);



// Creating SOAP envelope          

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

//You can comment that line if your web service is not .NET one.

envelope.dotNet = true;

envelope.setOutputSoapObject(request);

androidHttpTransport = new AndroidHttpTransport(url);

androidHttpTransport.debug = true;//need to be set true if it is a .net service

//SOAP calling webservice

androidHttpTransport.call(SOAP_ACTION, envelope);//making the soap call here

// Web service response

String result = envelope.getResponse().toString();//getting the service response here

return result;

}

catch (Exception e) {

// TODO: handle exception

return e.toString();


}

}

}


3. Define internet permission in manifest file :

   <uses-permission android:name="android.permission.INTERNET"/>

No comments:

Post a Comment