从Android

时间:2015-10-26 18:31:24

标签: android asp.net web-services ksoap2 android-ksoap2

我正在开发一个需要对ASP.Net Web服务进行Web服务调用的Android应用程序。我正在使用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)

2 个答案:

答案 0 :(得分:1)

这不应该编译,因为并非所有代码路径都在Web服务中返回一个字符串。

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

}

答案 1 :(得分:1)

它可以帮到你。

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

}