How to Call SOAP Web Services [Off] on Android

Close.The question is off-topic.It currently does not accept the answer. Learn more .

Want to improve this problem? Update Question Make it Stack Overflow Theme .

Closed 2 years ago.

I have difficulty finding good information on how to invoke standard SOAP / WSDL Web services using Android.All I could find was a very complex document and a reference to "kSoap2", followed by some information about using SAX Manually parse their contents.Okay, that's good, but it was 2008, so I think there should be some good libraries to call standard Web services.

Web services are basically just In NetBeans Created one.I want the IDE to support the generation of pipeline classes.I only need the simplest/most elegant way to connect from an Android-based phone based on WSDL Web services for.

#1st floor

A few months ago, I used the jax-ws Web service in a j2ee application, and we were using the CXF wsdl2java Generate WS client stubs from WSDL files and consume Web services using them.A few weeks ago, when I tried to use Web services in the same way on the android platform, I couldn't because there weren't all the "jax-ws" support classes in the android jar.At that time, I didn't find any tools to meet my requirements (if I didn't fail to do a valid Google search)-

  • Get the client stub from WSDL.
  • And invoke the service (java business request object) with some parameters.
  • Gets the response business object.

So I developed my own Android SOAP Client Generation Tool .You must follow these steps:

  • Get the WS Client Stub from WSDL and place it in your project.
  • Say "ComplexOperationService" to some services, instantiate the service, get the endpoint port, call the service method, and get a response from the Web service:

For example:

ComplexOperationService service = new ComplexOperationService( );
ComplexOperation port= service.getComplexOperationPort();    
SomeComplexRequest request = --Get some complex request----;    
SomeComplexResp resp = port.operate( request  );
  • You don't need to care about service/request/response classes or any other classes and methods, as you know they are all generated from WSDL.
  • Of course, you don't need to know soap action / envelop / namespace, etc.Just like the methods we developers have been invoking.

#2nd floor

If you encounter problems when invoking a Web service with android, you can use the following code to invoke the Web service and get a response.Make sure your Web service returns a response in a data table format.This code will help you if you use data from a SQL Server database.If you use MYSQL, you need to change one thing by simply replacing the sentence obj2=(SoapObject) obj1.getProperty("NewDataSet"); the word NewDataSet with obj2=(SoapObject) obj1.getProperty("NewDataSet"); through DocumentElement

void callWebService(){ 

private static final String NAMESPACE = "http://tempuri.org/"; // for wsdl it may be package name i.e http://package_name
private static final String URL = "http://localhost/sample/services/MyService?wsdl";
// you can use IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = "urn:" + METHOD_NAME;

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("parm_name", prm_value);// Parameter for Method
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;// **If your Webservice in .net otherwise remove it**
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service
                                                                                                         // Method
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Next task is to get Response and format that response
    SoapObject obj, obj1, obj2, obj3;
    obj = (SoapObject) envelope.getResponse();
    obj1 = (SoapObject) obj.getProperty("diffgram");
    obj2 = (SoapObject) obj1.getProperty("NewDataSet");

    for (int i = 0; i < obj2.getPropertyCount(); i++) { 
// the method getPropertyCount() and  return the number of rows
            obj3 = (SoapObject) obj2.getProperty(i);
            obj3.getProperty(0).toString();// value of column 1
            obj3.getProperty(1).toString();// value of column 2
            // like that you will get value from each column
        }
    }

If you have any questions about this, you can write to me.

#3rd floor

Perform the following steps through the SOAP method

In the WSDL file,

  • Create a SOAP request template for each request.

  • Then replace the value you want to pass in your code.

  • Publish this data to the service endpoint using the DefaultHttpClient instance.

  • Get the response stream, and finally

  • Parse the response stream using an XML Pull parser.

#4th floor

To invoke a SOAP Web service from android, try using this client

Don't forget to add ksoap2-android.jar to the Java build path

public class WsClient {
    private static final String SOAP_ACTION = "somme";
    private static final String OPERATION_NAME = "somme";
    private static final String WSDL_TARGET_NAMESPACE = "http://example.ws";
    private static final String SOAP_ADDRESS = "http://192.168.1.2:8080/axis2/services/Calculatrice?wsdl";

    public String caclculerSomme() {

        String res = null;
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);
        request.addProperty("a", "5");
        request.addProperty("b", "2");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

        try {
            httpTransport.call(SOAP_ACTION, envelope);
            String result = envelope.getResponse().toString();
            res = result;
            System.out.println("############# resull is :" + result);
        } catch (Exception exception) {
            System.out.println("########### ERRER" + exception.getMessage());
        }

        return res;
    }
}

#5th floor

Don't forget to add ksoap2.jar to your project and INTERNET permissions to the AndroidManifest file

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class WebserviceActivity extends Activity {

    private static final String NAMESPACE = "https://api.authorize.net/soap/v1/";
    private static final String URL ="https://apitest.authorize.net/soap/v1/Service.asmx?wsdl"; 
    private static final String SOAP_ACTION = "https://api.authorize.net/soap/v1/AuthenticateTest";
    private static final String METHOD_NAME = "AuthenticateTest";
    private TextView lblResult;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lblResult = (TextView) findViewById(R.id.tv);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        request.addProperty("name","44vmMAYrhjfhj66fhJN");
        request.addProperty("transactionKey","9MDQ7fghjghjh53H48k7e7n");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
            // SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;


            lblResult.setText(resultsRequestSOAP.toString());
            System.out.println("Response::"+resultsRequestSOAP.toString());


        } catch (Exception e) {
            System.out.println("Error"+e);
        }

    }
}

Tags: Android Java Google SQL

Posted on Fri, 10 Jan 2020 18:00:07 -0800 by Online Connect