从Android到vb.net Web服务发送复杂对象的列表/数组

时间:2014-05-15 00:21:52

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

我正在尝试使用kso​​ap从android发送一个Complex对象的数组/列表到vb.net webservice。我可以发送复杂的对象,但列表给我带来麻烦。

这是android代码

private class SampleTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            // if you want, start progress dialog here
        }

        @Override
        protected String doInBackground(String... urls) {
            String webResponse = "";
            try {
                final String NAMESPACE = "http://sample.org/";
                final String URL = "http://192.168.1.103:8081/MySampleService.asmx";
                final String SOAP_ACTION = "http://sample.org/SampleWS";
                final String METHOD_NAME = "SampleWS";

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                 List<MediSenseLog> logs =  new ArrayList<MediSenseLog>();

                 MediSenseLog log1 = new MediSenseLog();
                    log1.setID(Integer.parseInt("1"));
                    log1.setLogDate("2014-05-13");

                     MediSenseLog log2 = new MediSenseLog();
                        log1.setID(Integer.parseInt("2"));
                        log1.setLogDate("2014-05-14");

                    logs.add(log1);
                    logs.add(log2);

                    SoapObject soapLogs = new SoapObject(NAMESPACE, "logs");

                    for (MediSenseLog i : logs){
                        soapLogs.addProperty("MediSenseLog", i);
                    }
                    request.addSoapObject(soapLogs);


                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                envelope.addMapping(NAMESPACE, "MediSenseLog",new MediSenseLog().getClass());

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.debug = true;
                androidHttpTransport.call(SOAP_ACTION, envelope);
                Log.d("HTTP REQUEST ",androidHttpTransport.requestDump);
                Log.d("HTTP RESPONSE", androidHttpTransport.responseDump);
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                Log.d("ComplexWS", response.toString());
                webResponse = response.toString();

            } catch (Exception e) {
            }

            return webResponse;
        }

        @Override
        protected void onPostExecute(String result) {

            Toast.makeText(getApplicationContext(), "Synchonization Completed...",Toast.LENGTH_LONG).show();
            Log.d("ComplexWS","return val is "+result);

        }
    }

这是.net代码

 Public Class MediSenseLog
    Public ID As Integer
    Public LogDate As String

End Class

<WebMethod()> _
Public Function SampleWS(ByVal logs As MediSenseLog()) As String
    Return logs(1).LogDate.ToString()
End Function

这是使用requestdump的请求

 <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>
        <Sample xmlns="http://sample.org/" id="o0" c:root="1">
            <n0:logs i:type="n0:logs" xmlns:n0="http://sample.org/">
                <MediSenseLog i:type="n0:MediSenseLog">
                    <ID i:type="d:int">2</ID>
                    <LogDate i:type="d:string">2014-05-14</LogDate>
                </MediSenseLog>
                <MediSenseLog i:type="n0:MediSenseLog">
                    <ID i:type="d:int">0</ID>
                    <LogDate i:null="true" />
                </MediSenseLog>
            </n0:logs>
        </Sample>
    </v:Body>
</v:Envelope>

这是使用响应转储的响应

 <?xml version="1.0" encoding="utf-8"?><soap:Envelope        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <SampleResponse xmlns="http://sample.org/">
        <SampleResult />
    </SampleResponse>
</soap:Body>
</soap:Envelope>

返回值为空。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

发现错误

只需更改此

 log1.setID(Integer.parseInt("2"));
log1.setLogDate("2014-05-14");

log2.setID(Integer.parseInt("2"));
log2.setLogDate("2014-05-14");