android - 在代号一中使用 .NET Web 服务

标签 android ios windows-phone codenameone mobile-development

我是代号一的初学者,我必须使用网络服务( x.x.x.x/xxMobileWebService/xxService.asmx )。我已经使用 kSoap 在 android 原生开发中成功完成了这项工作。

那么有没有办法在代号一中访问 .Net 网络服务?

编辑 ---------------------------------- ---------------------------------------------- ------------------------

这是我的 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] 标签内的字符串数据('Kasthuriarachchi')。

最佳答案

如果您的网络服务返回一个 xml,您可以简单地使用 codenameone ConnectionRequestXMLParser。您甚至可以构建肥皂主体并使用 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>

关于android - 在代号一中使用 .NET Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869075/

相关文章:

Android ListView,不是所有的Touch-Events

c# - Windows Phone 模拟器未启动

.NET - 从用作网络摄像机的 Windows 手机获取视频流

android - 关闭 RecyclerView 子尺寸变化动画

java - 当没有条件可显示的项目时隐藏 recyclerview

ios - 架构 armv7 的 undefined symbol , Unresolved 常见修复

ios - CSS Scroll-Snapping 不断跳过它应该捕捉到的元素之一

c# - 键盘与文本框重叠

java - Android studio 无法编辑库

ios - 将 SDWebImage 用于带有可选图像 URL 的 UITableViewCell