I am making my first attempt at writing a BlackBerry App that consumes SOAP web services that embed JSON data, and have hit a challenge that's taking longer than I have to solve. I'd appreciate assistance on this. Thank you.
Below is the structure of the Web Service Request:
<logonrequest>
{
"userid":"username",
"password":"password",
"Timestamp":""
}
</logonrequest>
Below is the structure of the Web Service Response:
<return>
{
"userid":"username",
"displayName":"Firstname Lastname",
"accountBalance":0.0,
"statusCode":"0|SUCCESS",
"statusDesc":"Logon Success",
"sessionid":"12047CC560484D21BD328EC8F22814ED",
"lastTransactionTime":"20131130234326"
}
</return>
Below is my BlackBerry Java Code: package com.abc.app;
import java.io.IOException;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.xmlpull.v1.XmlPullParserException;
import a1.org2b.json.me.JSONException;
import a1.org2b.json.me.JSONObject;
public class MySoapAttempt extends MainScreen {
String serviceUrl = "http://www.abcappserver.tk:30740/abcJSONWebApp/abcJSONWebServiceV1?wsdl";
String serviceNamespace = "http://webapps.abc.abcConnect.com/";
String soapAction = "http://webapps.abc.abcConnect.com/logonrequest";
HttpTransport transport = new HttpTransport(serviceUrl + "/;deviceside=false;ConnectionType=mds-public");
SoapObject rpc = new SoapObject(serviceNamespace, "logonrequest");
// SoapObject result;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
public MySoapAttempt() {
envelope.bodyOut = rpc;
JSONObject container = new JSONObject();
try {
container.put("userid", "username");
container.put("password", "password");
container.put("Timestamp", "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rpc.addProperty("logonrequest", container.toString());
envelope.setOutputSoapObject(rpc);
try {
envelope.dotNet = false;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
transport.debug = true;
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
transport.call(soapAction, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
Dialog.alert(result.toString());
System.out.println("------=========-------- "+result);
} catch (IOException e) {
System.out.println("======= IOException =======");
System.out.println(e.getMessage());
e.printStackTrace();
} catch (XmlPullParserException e) {
System.out.println("======= XmlPullParserException =======");
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
} catch (Exception e) {
System.out.println("======= General Exception =======");
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
} finally {
System.out.println("=== >>> REQUEST: " + transport.requestDump);
System.out.println("===========================================");
System.out.println("=== >>> REQUEST: " + transport.responseDump);
add(new LabelField("Welcome To My SOAP Attempt"));
}
}
}
Below is the structure of the Web Service Console:
[127779.77] AM: Starting abc
[127779.79] AM: Started abc(483)
[127779.79] AM: Foreground is set: abc(483)
...
[127793.06] ======= XmlPullParserException =======
[127793.06] expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schema
[127793.06] s.xmlsoap.org/soap/envelope/}S:Fault>@1:324 in java.io.InputStreamReader@267621d3)
[127793.06] org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}
[127793.06] Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}S:Fault>@1:324 in java.io.InputS
[127793.06] treamReader@267621d3)
[127793.06] No stack trace
[127793.06] === >>> REQUEST: <?xml version="1.0" encoding="UTF-8"?><v:Envelope xmlns:i="http://www.w3.org/2001/X
[127793.06] MLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/so
[127793.06] ap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/
[127793.06] envelope/"><v:Header /><v:Body><n0:logonrequest id="o0" c:root="1" xmlns:n0="http://webapps.abc.
[127793.06] abcConnect.com/"><logonrequest i:type="d:string">{"userid":"username","password":"password","Timesta
[127793.06] mp":""}</logonrequest></n0:logonrequest></v:Body></v:En
[127793.06] velope>
[127793.06]
[127793.06] ===========================================
[127793.07] === >>> REQUEST: <?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.o
[127793.07] rg/soap/envelope/"><S:Body><S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"><faultcode>S
[127793.07] :Client</faultcode><faultstring>Cannot find dispatch me
[127793.07] thod for {http://webapps.abc.abcConnect.com/}logonrequest</faultstring></S:Fault></S:Body></S:E
[127793.07] nvelope>
I notice from the console output that my request string seems to be malformed. How can I have it send a request that looks exactly like the required sample request above? Thank you.
I can't answer this question from a SOAP perspective as I know nothing about SOAP. And I am not great on XML. But from an XML perspective, your XML appears to be badly formed. If you want to pass data that should NOT be interpreted as XML, then use CDATA. Otherwise the XML parser will try to interpret the JSON characters as XML which is it not.
For further information see here:
http://www.w3schools.com/xml/xml_cdata.asp
As I understood it, when you are using SOAP, you are expected to use XML, so another option would be to format your data for XML. It should be possible to create an equivalent XML structure to contain your JSON data and that will be parsed correctly.