JSON String returned from SOAP web service containing no records for table

拟墨画扇 提交于 2019-12-01 15:25:24

It's seem this problem relate to some specific symbol like : \ lead to soap parse error.

you can use some tool like Ethereal to analyzer the input stream and make sure you have received correctly. if it's right, may be you should encode you json data and then send it.

I just test it on my server(lamp)which return a JSONObject in body,and android use below code,work normally:

public static void testApi() {
        AndroidHttpTransport androidHttptt = new AndroidHttpTransport("http://172.16.0.178/1mobile/market/services.php");
        SoapObject request = new SoapObject(NAMESPACE, "check_error");
        // request.addProperty("data", "empty");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Element[] header = new Element[1];
        header[0] = new Element().createElement(NAMESPACE, "Credentials");
        envelope.dotNet = true;
        envelope.headerOut = header;
        envelope.setOutputSoapObject(request);

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(url);
        androidHttpTransport.setXmlVersionTag(XML_HEAD);
        try {
            androidHttptt.call("http://172.16.0.178/check_error", envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            Log.e("response", "response=" + response.toString());
        } catch (Exception e) {

        }
    }

with Etheral,I get the data like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:check_errorResponse xmlns:ns1="http://test.test.com">
<Result xsi:type="xsd:string">{&quot;checkrecord&quot;:[{&quot;rollno&quot;:&quot;abc2&quot;,&quot;percentage&quot;:40,&quot;attended&quot;:12,&quot;missed&quot;:34}],&quot;Table1&quot;:[]}</Result>
</ns1:check_errorResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can find the json data have been convered to

{&quot;checkrecord&quot;:[{&quot;rollno&quot;:&quot;abc2&quot;,&quot;percentage&quot;:40,&quot;attended&quot;:12,&quot;missed&quot;:34}],&quot;Table1&quot;:[]}

SOAP protocol uses XML as means of communication. Your Request will be converted to a XML String and the response from the server will be a XML String which the KSOap parses and gives you an Object. So it looks like the response from WebService is fine but there is a problem with KSoap parsing. So as you guys were discussing you can encode the String in the server and return it. Android already provides support for Base64 encoding/decoding.

In the webservice return the following instead of json.

Convert.ToBase64String (Encoding.UTF8.GetBytes (json));

This will return you an encoded string. So the response you will get be an encoded string. So in the android client side.

new String(android.util.Base64.decode(responseString.getBytes(),android.util.Base64.DEFAULT))

which will give u the decoded string.Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!