3

Codename One の初心者で、Web サービス( xxxx/ xxMobileWebService/ xxService.asmx )を使用する必要があります。kSoapを使用してAndroidネイティブ開発でこれを成功させました。

Codename One で .Net Web サービスにアクセスする方法はありますか?

編集-------------------------------------------------- -------------------------------------------------- -----

これが私のXML要素です:

<soap:envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
 <soap:body>
  <getcommonalldataresponse xmlns='http://tempuri.org/'>
   <getcommonalldataresult>
    <xs:schema xmlns:msprop='urn:schemas-microsoft-com:xml-msprop' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns='' id='NewDataSet'>
     <xs:element msdata:usecurrentlocale='true' msdata:isdataset='true' name='NewDataSet'>
      <xs:complextype>
       <xs:choice maxoccurs='unbounded' minoccurs='0'>
        <xs:element name='Table' msprop:refcursorname='REFCursor'>
         <xs:complextype>
          <xs:sequence>
           <xs:element name='NAME' type='xs:string' minoccurs='0' msprop:oradbtype='126'>
           </xs:element>
          </xs:sequence>
         </xs:complextype>
        </xs:element>
       </xs:choice>
      </xs:complextype>
     </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'>
     <newdataset xmlns=''>
      <table diffgr:id='Table1' msdata:roworder='0'>
       <name>
        'Kasthuriarachchi'
       </name>
      </table>
     </newdataset>
    </diffgr:diffgram>
   </getcommonalldataresult>
  </getcommonalldataresponse>
 </soap:body>
</soap:envelope>

取得したいのは、[name] タグ内の文字列データ ('Kasturiarachchi') です。

4

1 に答える 1

2

Web サービスが xml を返す場合は、単純に codenameoneConnectionRequestXMLParser. 石鹸本体を構築し、buildRequestBody を使用して投稿することもできます。

以下は、これを処理するために過去に使用したコードです。

final String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
       + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
       + "                 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
       + "                 xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
       + "    <soap12:Body>\n"
       + "        <DoStuff xmlns=\"http://tempuri.org/\">\n"
       + "            <SomeVariables>blablabla</SomeVariables>\n"
       + "        </DoStuff>\n"
       + "    </soap12:Body>\n"
       + "</soap12:Envelope>\n";

ConnectionRequest req = new ConnectionRequest() {
    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
        super.buildRequestBody(os);
        os.write(body.getBytes("utf-8"));
    }

    @Override
    protected void handleException(Exception err) {
        Dialog.show("", "Connection lost, please check your internet and try again", "Ok", null);
    }
};

req.setUrl("Your_URL_Here");
req.addRequestHeader("Content-Type", "text/xml; charset=utf-8"); //application/soap+xml
req.addRequestHeader("Content-Length", body.length() + "");
req.setPost(true);

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
req.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueueAndWait(req);
byte[] data = req.getResponseData();

try {
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    InputStreamReader reader = new InputStreamReader(bais);
    XMLParser parser = new XMLParser();
    Element elem = parser.parse(reader);
    String name = ((Element) elem.getDescendantsByTagName("name").firstElement()).getChildAt(0).getText();
    System.out.println(name);
} catch (Exception ex) {
    ex.printStackTrace();
}

nameタグの値を読み取るための XML 応答:

<soap:envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
 <soap:body>
  <getcommonalldataresponse xmlns='http://tempuri.org/'>
   <getcommonalldataresult>
    <xs:schema xmlns:msprop='urn:schemas-microsoft-com:xml-msprop' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns='' id='NewDataSet'>
     <xs:element msdata:usecurrentlocale='true' msdata:isdataset='true' name='NewDataSet'>
      <xs:complextype>
       <xs:choice maxoccurs='unbounded' minoccurs='0'>
        <xs:element name='Table' msprop:refcursorname='REFCursor'>
         <xs:complextype>
          <xs:sequence>
           <xs:element name='NAME' type='xs:string' minoccurs='0' msprop:oradbtype='126'>
           </xs:element>
          </xs:sequence>
         </xs:complextype>
        </xs:element>
       </xs:choice>
      </xs:complextype>
     </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'>
     <newdataset xmlns=''>
      <table diffgr:id='Table1' msdata:roworder='0'>
       <name>
        'Kasthuriarachchi'
       </name>
      </table>
     </newdataset>
    </diffgr:diffgram>
   </getcommonalldataresult>
  </getcommonalldataresponse>
 </soap:body>
</soap:envelope>
于 2016-01-19T08:01:49.570 に答える