Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xml - How to consume ASP.NET Web API from a Java ME Application

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. No 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 No 478</direccion>
<nroID>15586005</nroID>
<nroTelef/>
<razonSocial>AEDO YANQUI, MARGARITA</razonSocial>
<tipoPersona>N</tipoPersona>
</Customer>
<Customer>
<codigoCli>115</codigoCli>
<direccion>JR. AMALIA PUGA No 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 No 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...