2

I have created a java ME application (prototype) and now I need to consume my WEB API service from a MIDlet. First of all, is it possible to consume Web API services from MIDlets?? I have converted my WCF to Web API just so that I can make my J2ME app have access to my services in a more straightforward way. The problem is that I have no idea how to call my web API methods from a MIDlet. Have you ever done anything similar? Do you have any links that you can share??

EDIT:

I have found how to consume a method from Web API, but still don't know how to turn what I get from the web API into something I can really display on a mobile screen

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 an example of the XML 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 have also read that I should use kXML2 , but all the info is so confusing , the only good tutorial I have been able to find was this one, the problem is that it uses KXML, which according to this page is deprecated

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

P.S. Currently my services return XML , but if you know how to work with json objects in java ME , I could easily return json instead.

Thanks in advance.

Community
  • 1
  • 1
Axel
  • 1,674
  • 4
  • 26
  • 38
  • please I really need some help – Axel May 17 '14 at 12:28
  • http://stackoverflow.com/questions/2981296/json-parser-for-j2me – jmj May 18 '14 at 23:00
  • @JigarJoshi OK, it's seems to be what I'm looking for. Unfortunately I can't see any example of how to use this jar. – Axel May 18 '14 at 23:05
  • you can google for examples – jmj May 18 '14 at 23:07
  • @JigarJoshi Only one tutorial http://www.java-n-me.com/2010/11/java-me-and-json.html and the article this tutorial is based on, is not there anymore :( – Axel May 18 '14 at 23:23
  • @JigarJoshi From the code I added to my question, why do they have to turn the stream into an array of bytes?? Wans't this line `connection.openInputStream();` suppossed to return the whole XML??? – Axel May 18 '14 at 23:25
  • It looks like you got your XML content in byte[] response. Now you need to parse that response. Take a look at this other question: http://stackoverflow.com/questions/9890222/need-some-help-in-parsing-this-xml-in-j2me-platform – Telmo Pimentel Mota May 21 '14 at 11:17

1 Answers1

1

I think most JavaME developers do it simply by calling a website URL, like. e.g.

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

And then yourwebservice.aspx simply returns data.

For highscore system in the games I've worked on, I'd call something like

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

And it would output the highscores in plain text, which my MIDlet would then read.

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

mr_lou
  • 1,910
  • 2
  • 14
  • 26