How to use ASP.NET web API from Java ME application

I created a Java Java application (prototype) and now I need to use my WEB API service from MIDlet. First of all, is it possible to use the web API services from MIDlet? I converted my WCF to a Web API just to make the J2ME application more accessible to my services in a simpler way. The problem is that I have no idea how to call web API methods from a MIDlet. Have you ever done something like this? Do you have any links you can share?

EDIT:

I found how to use the method from the Web API, but still do not know how to turn what I get from the web API into something that I can really display on the screen of a mobile phone.

This is the code I'm using:

HttpConnection connection = null; InputStream is = null; final ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] response = null; try { connection = (HttpConnection)Connector.open("http://myminimarket/api/customers/GetCustomers", Connector.READ); connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1"); if (connection.getResponseCode() == HttpConnection.HTTP_OK) { is = connection.openInputStream(); if (is != null) { int ch = -1; while ((ch = is.read()) != -1) { bos.write(ch); } response = bos.toByteArray(); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bos != null) { bos.close(); } if (is != null) { is.close(); is = null; } if (connection != null) { connection.close(); connection = null; } } catch (Exception e2) { e2.printStackTrace(); } } 

And this is the XML example that I get from GetCustomers:

 <ArrayOfCustomer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WSWebAPI.Helpers"> <Customer> <codigoCli>30</codigoCli> <direccion>MCDO. SAN MARTIN PSTO. Nº 06</direccion> <nroID>26626315</nroID> <nroTelef>365548</nroTelef> <razonSocial>ABANTO CASTAÑEDA, PAULA</razonSocial> <tipoPersona>N</tipoPersona> </Customer> <codigoCli>61</codigoCli> <direccion> JR. SANTA TERESA DE JUORNET MZA. L. LOTE 11 (FRENTE AL QUINDE-COSTADO DE FARMACIA) </direccion> <nroID>10414741067</nroID> <nroTelef/> <razonSocial>ACUÑA SIFUENTES, ILZE SOLEDAD</razonSocial> <tipoPersona>N</tipoPersona> </Customer> <Customer> <codigoCli>69</codigoCli> <direccion>JR. JOSE GALVEZ Nº 478</direccion> <nroID>15586005</nroID> <nroTelef/> <razonSocial>AEDO YANQUI, MARGARITA</razonSocial> <tipoPersona>N</tipoPersona> </Customer> <Customer> <codigoCli>115</codigoCli> <direccion>JR. AMALIA PUGA Nº 1008 TELEF. 367878</direccion> <nroID>10266028356</nroID> <nroTelef/> <razonSocial>ALARCON ZEGARRA, EDULFO</razonSocial> <tipoPersona>N</tipoPersona> </Customer> <Customer> <codigoCli>144</codigoCli> <direccion>AV. EVITAMIENTO SUR Nº 1514</direccion> <nroID>10267292588</nroID> <nroTelef/> <razonSocial>ALCANTARA GARCIA EDESBITA</razonSocial> <tipoPersona>N</tipoPersona> </Customer> <Customer> <codigoCli>194</codigoCli> <direccion> JR. 6 DE JULIO MZ. "C" LOTE 7 URB. LUIS ALBERTO SANCHEZ </direccion> <nroID>26956665</nroID> <nroTelef>362648</nroTelef> <razonSocial>ALVARADO CARDENAS, CONSUELO SOLEDAD</razonSocial> <tipoPersona>N</tipoPersona> </Customer> </ArrayOfCustomer> 

Now I also read that I have to use kXML2, but all the information is so confusing, the only good tutorial I could find was this one , the problem is that it uses KXML, which is outdated according to this page

Please, if any of you have ever used KXML2, I really appreciate that you could help me.

PS My services are currently returning XML, but if you know how to work with json objects in java ME, I could easily return json instead.

Thanks in advance.

+2
source share
1 answer

I think most JavaME developers do this simply by calling a website URL, for example. eg.

 http://www.yourdomain.com/yourwebservice.aspx 

And then yourwebservice.aspx just returns the data.

For the recorder system in the games that I worked on, I would call something like

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

And he will be breaking records in plain text that my MIDlet will read.

Here are some examples of reading return values: http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/io/HttpConnection.html

+1
source

All Articles