简体   繁体   English

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

[英]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. 我正在开发一个Android应用,该应用需要对ASP.Net Web服务进行Web服务调用。 I am using the KSOAP2 client and this has been working but stopped working all of a sudden. 我正在使用KSOAP2客户端,它一直在工作,但是突然停止工作。

I have reversed every recent changes I made without success. 我撤消了最近所做的所有更改,但均未成功。

Here is the Android Code 这是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;
            }

And here is portion of the ASP.Net Web Service 这是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;
        }

} }

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)

This shouldn't compile since not all code paths return a string in your web service. 由于并非所有代码路径都在您的Web服务中返回字符串,因此不应编译。

Initialize the result variable in your web service and return the result outside of your if statement. 在Web服务中初始化结果变量,然后在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;

} }

Its may help you. 它可能会帮助您。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM