【问题标题】:Parsing XML using ksoap2使用 ksoap2 解析 XML
【发布时间】:2015-10-04 11:20:45
【问题描述】:

谁能帮我用ksoap2解析这个xml??

<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <GetTurnoverResponse
        xmlns="http://tempuri.org/">
        <GetTurnoverResult
                xmlns:a="http://schemas.datacontract.org/2004/07/blablabla"
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Turnover>
                <a:Amount>369.2000000</a:Amount>
                <a:CurrencyID i:nil="true"/>
                <a:DateAndTime>2010-10-01T10:00:00</a:DateAndTime>
                <a:ForeignAmount>461.50</a:ForeignAmount>
                <a:ForeignCurrencyID>Euro</a:ForeignCurrencyID>
                <a:StoreNumber>101</a:StoreNumber>
            </a:Turnover>
</GetTurnoverResult>
    </GetTurnoverResponse>
</s:Body>
</s:Envelope>

我不确定我是否得到任何回应。因此我无法解析它。

请求 XML 如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://schemas.datacontract.org/2004/07/ProFashionAllServices">
  <SOAP-ENV:Body>
   <ns1:GetTurnover>
     <ns1:sessionID>12674579771167587539</ns1:sessionID>
  <ns1:selection>
    <ns2:EndDate>2010-10-01</ns2:EndDate>
    <ns2:StartDate>2010-10-01</ns2:StartDate>
  </ns1:selection>
   </ns1:GetTurnover>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我的要求是这样的:

public class LoadData extends AsyncTask<String, String, Void> {

private static final String SOAP_ACTION = "http://tempuri.org/ITurnoverService/GetTurnover";
private static final String METHOD_NAME = "GetTurnover";
private static final String NAMESPACE = "http://tempuri.org/";
private String response;
private boolean network;

private Context mContext;

public LoadData(Context context) {
    mContext = context;
}
@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Toast toast = Toast.makeText(mContext, response, Toast.LENGTH_LONG);
    toast.show();
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Void doInBackground(String... arg0) {

    //String conString = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("url", "http://wsa.pfagroup.nl:1662/OOPServices/");
    String URL = "http://wsa.pfagroup.nl:1662/OOPServices/" +"TurnoverService.svc";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("sessionID", arg0[0]);
    request.addProperty("EndDate", arg0[1]);
    request.addProperty("StartDate", arg0[1]);

    Log.i("doINaaaaaaaaaaaa", arg0[1]);
    Log.i("doINaaaaaaaaaaaa", arg0[0]);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;
    HttpTransportSE httpTransport = new HttpTransportSE(URL);

    httpTransport.debug = true;
    try {
        httpTransport.call(SOAP_ACTION, envelope);
        network = true;
    } catch (HttpResponseException e) {
        // TODO Auto-generated catch block
        Log.e("HTTPAAAAA", e.getMessage());
        //e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IOAAAAA", e.getMessage());
        response = "Check Your Network Connection";
        network = false;
        //e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        Log.e("XMLAAAAA", e.getMessage());
        //e.printStackTrace();
    } //send request

    if(network){
        SoapObject result = null;
        try {
            result = (SoapObject )envelope.getResponse();
            //response = result.getProperty("GetTurnoverResult").toString();
            Log.i("responseeeeeeeeee",String.valueOf(result.getPropertyCount()));
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            Log.e("SOAPAAAA", e.getMessage());
            //e.printStackTrace();
        }
    }

    return null;
}

}

logcat 中显示的错误消息:

18610-18895/com.******.*** E/SOAPAAAA﹕ De server kan de aanvraag niet verwerken als gevolg van een interne fout. Voor meer informatie over de fout kunt u IncludeExceptionDetailInFaults inschakelen (vanuit ServiceBehaviorAttribute of vanuit het configuratiegedrag <serviceDebug>) voor de server zodat de uitzonderingsgegevens naar de client worden teruggestuurd, of u kunt tracering inschakelen. Raadpleeg hiertoe de documentatie van Microsoft .NET Framework SDK en controleer de traceringslogboeken voor de server.

我的要求错了吗??请帮忙

【问题讨论】:

    标签: android xml parsing ksoap2


    【解决方案1】:

    您的请求未设置为在命名空间中。创建 PropertyInfo 并将命名空间、类型、名称和值设置为 PropertyInfo。并且,将此对象添加到请求中。你必须对所有三个参数都这样做

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    PropertyInfo p = new PropertyInfo();
    p.setNamespace(NAMESPACE);
    p.setType(PropertyInfo.STRING_CLASS);
    p.setName("sessionID");
    p.setValue(arg0[0]);
    request.addProperty(p);
    
    SoapObject selection = new SoapObject(NAMESPACE,"selection");
    PropertyInfo EndDate = new PropertyInfo();
    EndDate.setNamespace("http://schemas.datacontract.org/2004/07/ProFashionAllServices");
    EndDate.setType(PropertyInfo.STRING_CLASS);
    EndDate.setValue("2010-10-01");
    selection.addProperty(EndDate);
    
    PropertyInfo StartDate = new PropertyInfo();
    StartDate .setNamespace("http://schemas.datacontract.org/2004/07/ProFashionAllServices");
    StartDate .setType(PropertyInfo.STRING_CLASS);
    StartDate .setValue("2010-10-01");
    selection.addProperty(StartDate );
    
    request.addSoapObject(selection);
    

    【讨论】:

    • 不。找不到 SoapObject.setNamespace 方法。但我想我正在连续添加三个属性。但那些是嵌套的。 StartDate 和 EndDate 在 sessionID 属性中。这是个问题吗??
    • 对不起,这个函数在propertyinfo中。我正在更新我的答案
    • 这也行不通。同样的错误。但我想我正在连续添加三个属性。但那些是嵌套的。 StartDate 和 EndDate 在 sessionID 属性中。这是个问题吗??
    • 是的,StartDate 和 EndDate 是嵌套的。请再次检查我的更新
    • 我没有尝试过,我不确定它是否有效。 ns2 是 ns2="schemas.datacontract.org/2004/07/ProFashionAllServices"。我认为您可以将其指定为字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多