xml – 如何从Java ME应用程序中使用ASP.NET Web API

前端之家收集整理的这篇文章主要介绍了xml – 如何从Java ME应用程序中使用ASP.NET Web API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个java ME应用程序(原型),现在我需要从MIDlet中使用我的WEB API服务.
首先,是否有可能从MIDlet中使用Web API服务?我已经将我的WCF转换为Web API,这样我就可以让我的J2ME应用程序以更直接的方式访问我的服务.问题是我不知道如何从MIDlet调用我的Web API方法.你有没有做过类似的事情?你有可以分享链接吗?

编辑:

我已经找到了如何从Web API中使用方法,但仍然不知道如何将我从Web API获得的内容转换为我可以在移动屏幕上真正显示内容

这是我正在使用的代码

  1. HttpConnection connection = null;
  2. InputStream is = null;
  3.  
  4. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  5.  
  6. byte[] response = null;
  7.  
  8. try {
  9. connection = (HttpConnection)Connector.open("http://myminimarket/api/customers/GetCustomers",Connector.READ);
  10. connection.setRequestMethod(HttpConnection.GET);
  11.  
  12. connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
  13.  
  14. if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
  15. is = connection.openInputStream();
  16.  
  17. if (is != null) {
  18. int ch = -1;
  19.  
  20. while ((ch = is.read()) != -1) {
  21. bos.write(ch);
  22. }
  23.  
  24. response = bos.toByteArray();
  25. }
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally {
  30. try {
  31. if (bos != null) {
  32. bos.close();
  33. }
  34.  
  35. if (is != null) {
  36. is.close();
  37. is = null;
  38. }
  39.  
  40. if (connection != null) {
  41. connection.close();
  42. connection = null;
  43. }
  44. } catch (Exception e2) {
  45. e2.printStackTrace();
  46. }
  47. }

这是我从GetCustomers获得的XML的一个例子:

  1. <ArrayOfCustomer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WSWebAPI.Helpers">
  2. <Customer>
  3. <codigoCli>30</codigoCli>
  4. <direccion>MCDO. SAN MARTIN PSTO. Nº 06</direccion>
  5. <nroID>26626315</nroID>
  6. <nroTelef>365548</nroTelef>
  7. <razonSocial>ABANTO CASTAÑEDA,PAULA</razonSocial>
  8. <tipoPersona>N</tipoPersona>
  9. </Customer>
  10. <codigoCli>61</codigoCli>
  11. <direccion>
  12. JR. SANTA TERESA DE JUORNET MZA. L. LOTE 11 (FRENTE AL QUINDE-COSTADO DE FARMACIA)
  13. </direccion>
  14. <nroID>10414741067</nroID>
  15. <nroTelef/>
  16. <razonSocial>ACUÑA SIFUENTES,ILZE SOLEDAD</razonSocial>
  17. <tipoPersona>N</tipoPersona>
  18. </Customer>
  19. <Customer>
  20. <codigoCli>69</codigoCli>
  21. <direccion>JR. JOSE GALVEZ Nº 478</direccion>
  22. <nroID>15586005</nroID>
  23. <nroTelef/>
  24. <razonSocial>AEDO YANQUI,MARGARITA</razonSocial>
  25. <tipoPersona>N</tipoPersona>
  26. </Customer>
  27. <Customer>
  28. <codigoCli>115</codigoCli>
  29. <direccion>JR. AMALIA PUGA Nº 1008 TELEF. 367878</direccion>
  30. <nroID>10266028356</nroID>
  31. <nroTelef/>
  32. <razonSocial>ALARCON ZEGARRA,EDULFO</razonSocial>
  33. <tipoPersona>N</tipoPersona>
  34. </Customer>
  35. <Customer>
  36. <codigoCli>144</codigoCli>
  37. <direccion>AV. EVITAMIENTO SUR Nº 1514</direccion>
  38. <nroID>10267292588</nroID>
  39. <nroTelef/>
  40. <razonSocial>ALCANTARA GARCIA EDESBITA</razonSocial>
  41. <tipoPersona>N</tipoPersona>
  42. </Customer>
  43. <Customer>
  44. <codigoCli>194</codigoCli>
  45. <direccion>
  46. JR. 6 DE JULIO MZ. "C" LOTE 7 URB. LUIS ALBERTO SANCHEZ
  47. </direccion>
  48. <nroID>26956665</nroID>
  49. <nroTelef>362648</nroTelef>
  50. <razonSocial>ALVARADO CARDENAS,CONSUELO SOLEDAD</razonSocial>
  51. <tipoPersona>N</tipoPersona>
  52. </Customer>
  53. </ArrayOfCustomer>

现在我还读到我应该使用kXML2,但所有的信息都是如此令人困惑,我能找到的唯一好的教程是this one,问题是它使用KXML,其中according to this page已被弃用

如果您有任何人曾经使用过KXML2,我真的很感谢您能帮助我.

附:目前我的服务返回XML,但如果你知道如何在java ME中使用json对象,我可以轻松返回json.

提前致谢.

我认为大多数JavaME开发人员只需通过调用网站URL即可.例如
  1. http://www.yourdomain.com/yourwebservice.aspx

然后yourwebservice.aspx只返回数据.

对于我参与过的游戏中的高分系统,我会称之为

  1. http://www.gamename.com/webservice.PHP?action=gethighscores

并且它将以纯文本形式输出高分,然后我的MIDlet将读取该高分.

以下是读取返回值的一些示例:http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/io/HttpConnection.html

猜你在找的XML相关文章