mercredi 28 septembre 2016

How to call a SOAP web service from an Android mobile application

From the past few days, I have been working on an Android code to call distant web service. I am using ksoap libraries for Android to call my SOAP web service. However, I feel there is something wrong in my code as the response I get when I call the web service from my app "Error calling WS". I tried debugging my Android code but I am still not able to solve my problem. Please can someone tell me what's wrong ? to call my WS i send 3 params (CountryCode, ApplicationCode and ApplicationID) Here is my Android code I have implemented till now :

====>

package com.hp.hpsupport.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.hp.hpsupport.R;
import com.hp.hpsupport.model.Credential;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.Serializable;

public class MyDevices extends Fragment {

    private final String NAMESPACE = "http://tempuri.org/";
    private final String URL = "http://ift.tt/2cBUGC9";
    private final String SOAP_ACTION = "tns:IWCFGNAgreementServicev1/IWCFGNAgreementService/GetAgreementServiceCatalog";
    private final String METHOD_NAME = "GetAgreementServiceCatalog";

    private String TAG = "PGGURU";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_devices, null);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText countryCode = (EditText) view.findViewById(R.id.CountryCode);
                EditText applicationCode = (EditText) view.findViewById(R.id.ApplicationCode);
                EditText applicationID = (EditText) view.findViewById(R.id.ApplicationID);

                new AsyncCallWS().execute(countryCode.getText().toString(),applicationCode.getText().toString(),applicationID.getText().toString());
            }
        });

    }

    private class AsyncCallWS extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            return GetAgreementServiceCatalog(params[0], params[1], params[2]);
        }

        @Override
        protected void onPostExecute(String result) {
            Log.i(TAG, "onPostExecute");
            Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

        // appel webservice soap
        public String GetAgreementServiceCatalog(String countryCode,String applicationCode,String applicationID) {
            //Create request
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //Property which holds input parameters
            PropertyInfo tns = new PropertyInfo();

            tns = new PropertyInfo();
            tns.setName("CountryCode");
            tns.setValue(countryCode);
            request.addProperty(tns);

            //Property which holds input parameters
            tns = new PropertyInfo();
            tns.setName("Credential");

            Credential cred = new Credential();
            cred.ApplicationCode = applicationCode;
            cred.ApplicationID = applicationID;


            tns.setValue(cred);
            tns.setType(Credential.class);
            // tns.setElementType(PropertyInfo.OBJECT_TYPE);
            // request.addProperty(tns);

            //Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            //Set output SOAP object
            envelope.setOutputSoapObject(request);
            //Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                //Invole web service
                androidHttpTransport.call(SOAP_ACTION, envelope);
                //Get the response
                SoapObject response = (SoapObject) envelope.getResponse();
                //return country
                SoapObject so = (SoapObject) response.getPrimitiveProperty("UsageServices");
                return so.getPrimitivePropertyAsString("Description");
                //return Successful
                //return response.getPrimitivePropertyAsString("ReturnText");

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

            return "Error calling WS";
        }
    }
}

===> And my XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/RelativeLayout"
    android:clickable="true">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Get Catalog SF"
        android:textSize="30dp" />



    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="US"
        android:id="@+id/CountryCode"
        android:layout_below="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="AFE48CB7DB40CB0CEF1F372DC9BB9598392684DBD8B30434E9CF4E036F4FE93F"
        android:id="@+id/ApplicationCode"
        android:layout_below="@+id/CountryCode"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="SFMO"
        android:id="@+id/ApplicationID"
        android:layout_below="@+id/ApplicationCode"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Catalog"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView" />
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire