Eyes
Eyes

Reputation: 7

How to pass parameters to SOAP webservices

Hi i ma new to android i want to pass some parameters to the soap web services but it throwing NPE while getting response.i made in comment where exactly im getting NPE. please provide me some solution

Here is my XMl

<soap:Body>
<CreateNewTask xmlns="http://tempuri.org/">
  <description>string</description>
  <userid>string</userid>
  <task>
    <Id>guid</Id>
    <TaskMessage>string</TaskMessage>
    <UserId>guid</UserId>
    <AssignTaskTo>guid</AssignTaskTo>
    <TaskStatus>boolean</TaskStatus>
    <AssignDate>string</AssignDate>
    <CompletionDate>dateTime</CompletionDate>
  </task>
  <assigntoId>string</assigntoId>
  <comment>string</comment>
</CreateNewTask>

Here is my AsynchTask for soap

public class New_Task extends AsyncTask<String, Void, String> {
    String result = null;
    Object resultRequestSOAP = null;

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);

        request.addProperty("description", Singleton.Task_message);         
        request.addProperty("userid", Singleton.user_id);           
        request.addProperty("Id", Singleton.user_id);           
        request.addProperty("TaskMessage", Singleton.Task_message);         
        request.addProperty("UserId", Singleton.user_id);           
        request.addProperty("AssignTaskTo", Singleton.user_name);           
        request.addProperty("TaskStatus", "false");
        request.addProperty("AssignDate", "2013-12-18T11:12:08.000");           
        request.addProperty("CompletionDate", "2013-12-18T11:12:08.000");           
        request.addProperty("assigntoId", Singleton.user_id);                       
        request.addProperty("comment", comment.getText().toString());           

        // Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        try {

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_ACTION1, envelope);
            SoapObject soap_result = (SoapObject) envelope.getResponse();
            System.out.println("soap_result  " + soap_result);          
            String requestDumpString = androidHttpTransport.requestDump;                
            System.out.println("requestDump : " + requestDumpString);               
            result = soap_result.toString(); // here i am getting NPE
            System.out.println("result  " + result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

here is my logcat result NPE

 12-18 13:35:59.052: W/System.err(13924): java.lang.NullPointerException
12-18 13:35:59.062: W/System.err(13924):    at com.example.woosuite.Woosuite_NewTask$New_Task.doInBackground(Woosuite_NewTask.java:183)
12-18 13:35:59.062: W/System.err(13924):    at com.example.woosuite.Woosuite_NewTask$New_Task.doInBackground(Woosuite_NewTask.java:1)
12-18 13:35:59.062: W/System.err(13924):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-18 13:35:59.062: W/System.err(13924):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-18 13:35:59.062: W/System.err(13924):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-18 13:35:59.072: W/System.err(13924):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-18 13:35:59.072: W/System.err(13924):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-18 13:35:59.072: W/System.err(13924):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-18 13:35:59.072: W/System.err(13924):    at java.lang.Thread.run(Thread.java:856)

Thanks in advance

Upvotes: 0

Views: 3924

Answers (3)

Hamid
Hamid

Reputation: 1563

However the existing answer is correct but it is better to send parameters to API using AsyncTask for performance issues. so you can do this by using following codes:
In this sample, the version Number and device Id will be sent to the soap webservice:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallWebService cws = new CallWebService();
                cws.execute();
            }
        });

    }

    public static String DebugLog = "MyApp_Log";
    class CallWebService extends AsyncTask<String, String, Boolean> {

        String resultStr = "";

        @Override
        protected Boolean doInBackground(String... params) {
            Log.i(DebugLog, "sending params to server...");


            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

            PropertyInfo deviceIdInfo = new PropertyInfo();
            deviceIdInfo.setName("deviceId");
            deviceIdInfo.setValue("Idqwersdfsd");
            deviceIdInfo.setType(String.class);
            request.addProperty(deviceIdInfo);

            PropertyInfo versionNumberInfo = new PropertyInfo();
            versionNumberInfo.setName("versionNumber");
            versionNumberInfo.setValue("12");
            versionNumberInfo.setType(String.class);
            request.addProperty(versionNumberInfo);


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.debug = true;
                androidHttpTransport.call(SOAP_ACTION1, envelope);

                if (envelope.bodyIn instanceof SoapFault) {
                    String str = ((SoapFault) envelope.bodyIn).faultstring;
                    Log.i(DebugLog, "bodyIn is instance of soapFault:" + str);
                    return false;
                } else {
                    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

                    if (resultsRequestSOAP != null) {
                        resultStr = resultsRequestSOAP.getProperty(0).toString();
                        Log.i(DebugLog, "response is:" + resultStr);
                        return true;

                    } else {
                        resultStr = "No Response";
                        Log.i(DebugLog, "server no responsed any thing..");
                        return false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(DebugLog, "Exception in registration:" + e.toString());
                resultStr = e.toString();
                return false;
            }
    }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {

            } else {
                Log.i(DebugLog, "some message");

            }
        }
    }

Upvotes: 0

Amol Suryawanshi
Amol Suryawanshi

Reputation: 2184

I used following code for passing arguments to method

PropertyInfo info= new PropertyInfo();

//Set Name info.setName("prefixText"); //Set Value info.setValue(mobileNum); //Set dataType info.setType(String.class); //Add the property to request object request.addProperty(info);

info = new PropertyInfo();
//Set Name
info.setName("count");
//Set Value
info.setValue(5);
//Set dataType
info.setType(integer.class);
//Add the property to request object
request.addProperty(info);

in this way we can specify the arguments datatype so, try it.alter the code according to need

Upvotes: 1

PankajAndroid
PankajAndroid

Reputation: 2707

Please try this with some changes

    SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);

    PropertyInfo info = new PropertyInfo();
    info.setName("strInputData"); // .Net Funcation argument key
    info.setType(String.class);
    request.addProperty(info);

    request.addProperty("description", Singleton.Task_message);         
    request.addProperty("userid", Singleton.user_id);           
    request.addProperty("Id", Singleton.user_id);           
    request.addProperty("TaskMessage", Singleton.Task_message);         
    request.addProperty("UserId", Singleton.user_id);           


    // Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;
    try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION1, envelope);

        SoapObject soap_result = (SoapObject) envelope.getResponse();
        System.out.println("soap_result  " + soap_result);              
        String result = soap_result.toString(); // here i am getting NPE
        System.out.println("result  " + result);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

i had post data with json value while you are posting data with xml so there is some change

Upvotes: 2

Related Questions