使用Android中的KSoap库读取Web服务结果时出错

时间:2013-03-11 16:11:51

标签: android

我正在使用Ksoap库从webservice读取xml。以下是我使用过的代码:

private static String SOAP_ACTION_Transaction = "http://tempuri.org/Trackdata";
    private static String NAMESPACE_Transaction= "http://tempuri.org/";
    private static String METHOD_NAME_Transaction = "Trackdata";
    private static String URL_Transaction = "https://devraj.charge.com/process/getAndroidData.asmx?WSDL";

我使用asyn任务调用showdetails()方法。

   public void showdetails()
        {
            String result = "";
            SoapObject resultRequestSOAP = null;
            SoapObject resultRequestSOAPbody =null;
            SoapObject request = new SoapObject(NAMESPACE_Transaction, METHOD_NAME_Transaction);

            request.addProperty("Type","payment");
            request.addProperty("Id",53);
            request.addProperty("amount","0.02");
            request.addProperty("CCExpMonth",11);
            request.addProperty("CCExpYear",22);
            request.addProperty("Source","gg");
            request.addProperty("profileId",748);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Transaction);
            androidHttpTransport.debug = true;

            try 
            {
                androidHttpTransport.call(SOAP_ACTION_Transaction, envelope);
                String requestDumpString = androidHttpTransport.requestDump;
                System.out.println("requestDump : " + requestDumpString);
                resultRequestSOAP = (SoapObject) envelope.getResponse(); // Output received
                result = resultRequestSOAP.getProperty(0).toString(); // Result string
                 System.out.println("OUTPUT : " + result);


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

        }

但是我收到以下错误:

SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.

在此声明中: resultRequestSOAP =(SoapObject)envelope.getResponse(); 有人可以告诉我,我已经解析了其他web服务的错误,但工作正常但是对于这个我得到了这个错误。 以下是我的xml数据的一部分:

<?xml version="1.0" encoding="utf-8"?>
<ClsProcessData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <ProfileId>748</ProfileId>
  <Id>53</MerchantId>
  <InvoiceNum>53-375</InvoiceNum>
  <InvoiceDescription />
  <amount>0.02</InvoiceAmount>
  <CCName />
  </ClsProcessData>

任何帮助将不胜感激。如果有人能告诉我错误,请感谢。

1 个答案:

答案 0 :(得分:0)

SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
String responseLocal = resultsRequestSOAP.toString();

尝试上述而不是resultRequestSOAP =(SoapObject)envelope.getResponse();

 if (envelope.bodyIn instanceof SoapFault) {
    String str= ((SoapFault) envelope.bodyIn).faultstring;
    Log.i("", str);
} else {
    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
    Log.d("WS", String.valueOf(resultsRequestSOAP));
}

或者,如果您的回答是原始的,请尝试

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