从 Android 调用 .ASP.Net Web 服务时出错

Error Calling .ASP.Net Web Service from Android

我正在开发一个 Android 应用程序,它需要对 ASP.Net Web 服务进行 Web 服务调用。我正在使用 KSOAP2 客户端,它一直在工作,但突然停止工作了。

我已经撤销了我最近所做的所有更改,但没有成功。

这是Android代码

 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;
            }

这里是 ASP.Net Web 服务的一部分

[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;
        }

}

我可能遗漏了什么或者我可能在哪里引入了错误。始终错误消息是相同的

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)

这不应该编译,因为并非所有代码路径 return 您的 Web 服务中的字符串。

在您的网络服务中初始化结果变量,并return在您的 if 语句之外初始化结果。

     [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;

}

可能对你有帮助。

package databaseconnect.databaseconnect;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;     
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE; 
public class MainActivity extends Activity {

private static String SOAP_ACTION = "http://tempuri.org/Service/XXXXXX";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "MethodName";
private static String URL = "http://Example.com/Service1.svc?wsdl";
Button Save,Clear;
EditText name,mobile,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = (EditText) findViewById(R.id.editText);
    mobile = (EditText) findViewById(R.id.editText2);
    email = (EditText) findViewById(R.id.editText3);
    Save=(Button)findViewById(R.id.button);
    Clear=(Button)findViewById((R.id.clear));
    Save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AsyncCallWS task = new AsyncCallWS();
            task.execute();
        }
    });
    Clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name.setText("");
            mobile.setText("");
            email.setText("");
        }
    });

}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params)
    {


        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("name", name.getText().toString());
        request.addProperty("mobile", mobile.getText().toString());
        request.addProperty("email", email.getText().toString());
        //request.addProperty("name", "RamcoSystems");
       // request.addProperty("mobile", "044-0004972");
        //request.addProperty("email", "google@ramco.com");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);
        String res=null;
        try
        {

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,180000);
            androidHttpTransport.debug=true;
            androidHttpTransport.call(SOAP_ACTION, envelope);

        } catch (Exception e)
        {
            name.setText(e.toString());
        }

        return  null;
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}