-->

KSoap2 SoapObject对象引用未设置为一个实例(KSoap2 SoapObject Ob

2019-07-18 17:06发布

我认为这是一个简单的问题,其他人可能能够提供缺少的环节来。 我有一个工作流 WCF服务,我的.NET客户端可以正常通信。 我已经打开AppFabric的跟踪下能看服务呼叫正确执行,所以我感觉很舒服的服务是没有问题的。 我的Android调用从未碰到的第一个跟踪点。

在Android中使用kso​​ap2(2.6.5),当我把此行中我得到一个对象引用未设置错误:

SoapObject response = (SoapObject)envelope.getResponse();

对我来说,这似乎是简单的信封必须为空,当我们试图访问它的exec GETRESPONSE我们有我们的错误,但在调试器中它显示为有效的对象。 此外,我无法找到的文档来解释每教程中,我已经看到了这种方式那么....?

有一件事情,不同的是,在.NET中我将创建一个对象传递让.NET序列化,我的对象。 在这里,我补充几个参数,这些参数都是字符串,但枚举我会告诉,因为它是一个可能的罪魁祸首。 这是我的完整的Java实现:

String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "LiftDataExchange";
String SOAP_ACTION = "http://tempuri.org/ILiftDataExchange/ProcessDataRequest";
String URL = "http://www.icyarmtesting.com/LiftDataExchange.xamlx";
String SOAP_REMOTE_NAMESPACE = "http://schemas.datacontract.org/2004/07/IronMikeDataExchangeWorkflowService.Enum";

String X = "";
ExchangeEnumerations ExrcsEnum = ExchangeEnumerations.GetNextWorkout;

//Initialize soap request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//add parameters
PropertyInfo piAct = new PropertyInfo();
piAct.setName("Action");
piAct.setValue(ExrcsEnum);
piAct.setType(ExchangeEnumerations.class);
request.addProperty(piAct);

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(SOAP_REMOTE_NAMESPACE, "ExchangeEnumerations", ExchangeEnumerations.class, ExchangeEnumerationsEnumClass.getInstance());
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

//Needed to make the internet call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try 
    {          
  //this is the actual part that will call the webservice
  androidHttpTransport.call(SOAP_ACTION, envelope); 
  SoapObject response = (SoapObject)envelope.getResponse();
  X = response.getProperty(0).toString();
}
catch (Exception e)...... 

这里是我的要求转储:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <LiftDataExchange xmlns="http://tempuri.org/" id="o0" c:root="1">
            <UserName i:type="d:string">STERILIZED_OUT</UserName>
            <Password i:type="d:string">STERILIZED_OUT</Password>
            <ExerciseProgramID i:type="d:string">STERILIZED_OUT</ExerciseProgramID>
            <IncomingWorkoutData i:type="d:string">STERILIZED_OUT</IncomingWorkoutData>
            <Action i:type="n0:ExchangeEnumerations" xmlns:n0="http://schemas.datacontract.org/2004/07/IronMikeDataExchangeWorkflowService.Enum">GetNextWorkout</Action>
        </LiftDataExchange>
    </v:Body>
</v:Envelope>

这里是我的回应转储:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
            <faultstring xml:lang="en-US">Object reference not set to an instance of an object.</faultstring>
            <detail>
                <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <HelpLink i:nil="true"/>
                <InnerException i:nil="true"/>
                <Message>Object reference not set to an instance of an object.</Message>                
                <StackTrace>   at System.ServiceModel.Activities.WorkflowOperationContext.HandleEndResumeBookmark(IAsyncResult result)&#xD; at System.ServiceModel.Activities.WorkflowOperationContext.OnResumeBookmark()&#xD; at System.ServiceModel.Activities.WorkflowOperationContext..ctor(Object[] inputs, OperationContext operationContext, String operationName, Boolean performanceCountersEnabled, Boolean propagateActivity, Transaction currentTransaction, WorkflowServiceInstance workflowInstance, IInvokeReceivedNotification notification, WorkflowOperationBehavior behavior, ServiceEndpoint endpoint, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Description.WorkflowOperationBehavior.WorkflowOperationInvoker.OnBeginServiceOperation(WorkflowServiceInstance workflowInstance, OperationContext operationContext, Object[] inputs, Transaction currentTransaction, IInvokeReceivedNotification notification, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.PerformOperation()&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.HandleEndGetInstance(IAsyncResult result)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.Process()&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult..ctor(ControlOperationInvoker invoker, Object[] inputs, IInvokeReceivedNotification notification, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.InvokeBegin(Object instance, Object[] inputs, IInvokeReceivedNotification notification, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.NullReferenceException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>

更新:

切换到Edwin的方法调用了相同的结果。 所以,我从KSoap2 2.6.5 3.0.0-RC4升级,现在我得到一个NullRefException。 与先前的版本我会打的服务,并抛出对象未设置错误。 有了这个版本我不打的服务和扔NullRef。 该代码是改变是我使用的KSOAP的版本相同的唯一的事。 我写了一个简单的服务(记住这些是工作流服务),需要一个字符串,它的变化,并且踢一个字符串退了出去。

FWIW这里是我的代码来拨打电话:

String SOAP_ACTION = "http://tempuri.org/";
    String OPERATION_NAME="ChangeName";
    final String WSDL_TARGET_NAMESPACE = SOAP_ACTION;
    final String SOAP_ADDRESS= "http://www.icyarmtesting.com/ServiceTest/Service1.xamlx";
    Exception exception;
    String ErrorMsg="";
    String TEST_URL="" ;

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);

    //PropertyInfo piName = new PropertyInfo();
    //piName.setName("UserName");
    //piName.setValue("ramjet");
    //piName.setType(String.class);
    //request.addProperty(piName);
    request.addProperty("UserName", "ramjet");

    SoapSerializationEnvelope  envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.dotNet= true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport =null;
    if(!TEST_URL.equals(""))
        httpTransport = new HttpTransportSE(TEST_URL);
    else
        httpTransport = new HttpTransportSE(SOAP_ADDRESS);



    Object Response =null;
     try
     {
        httpTransport.call(SOAP_ACTION + "IService/" + OPERATION_NAME, envelope);
        Response = envelope.getResponse();
      }
     catch (SocketTimeoutException ex) 
     {
         ErrorMsg="Unable to connect";
          Response=null;
         exception=ex;
      }
      catch (IOException ie) 
      {
       ErrorMsg="Unable to connect";
      Response=null;
       exception=ie;
      }
       catch (Exception e) 
       {
        ErrorMsg="Unable to connect";
        Response=null;
        exception=e;
     }

Answer 1:

更改以下:

SoapObject response = (SoapObject)envelope.getResponse();
  X = response.getProperty(0).toString();

成为:

SoapObject response = (SoapObject)envelope.bodyIn;
                if(response != null)
                {
                      X=response.getProperty(0).toString();
                }


Answer 2:

这是我使用的调用Web服务的类希望这可以帮助你

public class CallSOAP {

private String SOAP_ACTION="your action url";
private String OPERATION_NAME="";//The web method OR web service you are going to call
private final String WSDL_TARGET_NAMESPACE=SOAP_ACTION;
private  final String SOAP_ADDRESS=Configuration.WEB_SERVICE_URL;//ip-address of serever where you host your service 
private Exception exception;
private String ErrorMsg="";
private String TERST_URL="" ;
public Object call(String MethodeName,ArrayList<PropertyInfo> Parameters){
  this.OPERATION_NAME=MethodeName;
  SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
 if(Parameters != null && Parameters.size()>0){
   for(PropertyInfo propertyInfo : Parameters){
      request.addProperty(propertyInfo);
 }
}

SoapSerializationEnvelope  envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet= true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport =null;
if(!this.TERST_URL.equals(""))
  httpTransport = new HttpTransportSE(this.TERST_URL);
else
  httpTransport = new HttpTransportSE(SOAP_ADDRESS);
 Object Response =null;
 try{
    httpTransport.call(SOAP_ACTION+OPERATION_NAME, envelope);
    Response=envelope.getResponse();
  }
 catch (SocketTimeoutException ex) {
     this.ErrorMsg="Unable to connect";
      Response=null;
     this.exception=ex;
  }
  catch (IOException ie) {
   this.ErrorMsg="Unable to connect";
  Response=null;
   this.exception=ie;
  }
   catch (Exception e) {
    this.ErrorMsg="Unable to connect";
    Response=null;
    this.exception=e;
 }
      return Response;
   }
}


Answer 3:

我知道它来不及回答这个问题,但我面临同样的问题,在我的代码是我输入的property.setname()不正确..确保你写的方法参数的正确名称(复制过去,它从问题WSDL)我使用kso​​ap2 3.1.1顺便说一句!



Answer 4:

我使用下面的函数在我的课与SOAP Web服务的工作。

public static String DeleteGame(String GameIDValue, String PlayerIdValue) {

        String responce = null; 

        SoapObject request = new SoapObject(SOAP_NAMESPACE, SOAP_METHOD_DeleteGame);

        PropertyInfo GameId = new PropertyInfo();
        PropertyInfo PlayerId = new PropertyInfo();

        GameId.setName("gameid");
        GameId.setValue(GameIDValue);

        PlayerId.setName("PlayerID");
        PlayerId.setValue(PlayerIdValue);

        request.addProperty(GameId);
        request.addProperty(PlayerId);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE aht = new HttpTransportSE(SOAP_URL);
        try {
            aht.call(SOAP_ACTION_DeleteGame, envelope);
            SoapPrimitive LoginResult;
            LoginResult = (SoapPrimitive)envelope.getResponse();
            System.out.println("=================Delete Game Results: "+LoginResult.toString());
            //System.out.println(LoginResult.toString());
            responce = LoginResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        return responce;
    }


文章来源: KSoap2 SoapObject Object Reference not set to an instance