Search code examples
androidasp.netweb-servicesksoap2android-ksoap2

Error Calling .ASP.Net Web Service from Android


I am working on an Android app that needed to make a web service call to an ASP.Net Web Service. I am using the KSOAP2 client and this has been working but stopped working all of a sudden.

I have reversed every recent changes I made without success.

Here is the Android Code

 private final String NAMESPACE = "http://www.domain.com/";
        private final String URL = "https://www.domain.com/WebServices/LicenseActivationService.asmx";
        private final String SOAP_ACTION = "http://www.domain.com/";
        private String LOG_TAG = "ACTIVATION";
        private String methName = "ActivateLicense";
    String resultJSON = "";
                ActivationDTO activation = new ActivationDTO();
                activation.setLoginEmail(mEmail);
                activation.setLoginPassword(mPassword);
                activation.setDeviceId(getDeviceId(mcontext));


                //convert the activation object to a JSON String so it can be sent to the
                //web service
                Gson gson = new Gson();

                String jsonActivationRecord = gson.toJson(activation);

                //make web service call
                // Create request
                SoapObject request = new SoapObject(NAMESPACE, methName);
                // Property which holds input parameters
                PropertyInfo paramPI = new PropertyInfo();
                // Set Name
                paramPI.setName("serializedUserInfo");
                // Set Value
                paramPI.setValue(jsonActivationRecord);
                // Set dataType
                paramPI.setType(String.class);
                // Add the property to request object
                request.addProperty(paramPI);
                // 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 {
                    // Invoke web service
                    androidHttpTransport.call(SOAP_ACTION+methName, envelope);
                    // Get the response
                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                    // Assign it to static result
                    resultJSON = response.toString();

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




                // TODO: register the new account here.
                return resultJSON;
            }

And here is portion of the ASP.Net Web Service

[WebService(Namespace = "http://www.domain.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)] 
 public class LicenseActivationService : System.Web.Services.WebService
{


    [WebMethod]
    public string ActivateLicense(string serializedUserInfo)
    {

        ActivationDto activationDTO = JsonConvert.DeserializeObject<ActivationDto>(serializedUserInfo);
        String result;


        if (activationDTO == null)
        {
            //return invalid input
            activationDTO.IsActivated = false;
            activationDTO.Result = "Input is empty";
            result = JsonConvert.SerializeObject(activationDTO);
            return result;
        }

}

What could I be missing or where could I have introduced a bug. Consistently the error message is the same

W/System.err: SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@432babe8
10-26 11:17:43.124 6809-7723/com.domain.app W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:137)

Solution

  • This shouldn't compile since not all code paths return a string in your web service.

    Initialize the result variable in your web service and return the result outside of your if statement.

         [WebMethod]
    public string ActivateLicense(string serializedUserInfo)
    {
    
        ActivationDto activationDTO = JsonConvert.DeserializeObject<ActivationDto>(serializedUserInfo);
        String result = "";
    
    
        if (activationDTO == null)
        {
            //return invalid input
            activationDTO.IsActivated = false;
            activationDTO.Result = "Input is empty";
            result = JsonConvert.SerializeObject(activationDTO);
    
        } 
        return result;
    

    }