BlackBerry SOAP消费

时间:2013-12-03 02:56:39

标签: soap blackberry

我正在尝试编写一个使用嵌入JSON数据的SOAP Web服务的BlackBerry App,并且遇到了比我必须解决的问题花费更长时间的挑战。我很感激这方面的帮助。谢谢。

以下是Web服务请求的结构:

<logonrequest>
    {
    "userid":"username",
    "password":"password",
    "Timestamp":""
    }
</logonrequest>

以下是Web服务响应的结构:

<return>
    {
        "userid":"username",
        "displayName":"Firstname Lastname",
        "accountBalance":0.0,
        "statusCode":"0|SUCCESS",
        "statusDesc":"Logon Success",
        "sessionid":"12047CC560484D21BD328EC8F22814ED",
        "lastTransactionTime":"20131130234326"
    }
</return>

以下是我的BlackBerry Java代码:     包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"));
        }
    }

}

以下是Web服务控制台的结构:

[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>

我从控制台输出中注意到我的请求字符串似乎格式不正确。如何让它发送一个看起来与上面所需的样本请求完全相同的请求?谢谢。

1 个答案:

答案 0 :(得分:0)

我无法从SOAP角度回答这个问题,因为我对SOAP一无所知。而且我对XML不太了解。但是从XML的角度来看,您的XML看起来很糟糕。如果要传递不应解释为XML的数据,请使用CDATA。否则,XML解析器将尝试将JSON字符解释为XML,而不是。

有关详细信息,请参阅此处:

http://www.w3schools.com/xml/xml_cdata.asp

据我了解,当您使用SOAP时,您需要使用XML,因此另一种选择是格式化XML数据。应该可以创建一个等效的XML结构来包含您的JSON数据,并且这些数据将被正确解析。