如何使用来自一个Java ME应用程序的ASP.NET Web API [英] How to consume ASP.NET Web API from a Java ME Application

查看:162
本文介绍了如何使用来自一个Java ME应用程序的ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Java ME应用程序(原型),现在我需要消耗从MIDlet我的Web API服务。
首先,是有可能从MIDlet的使用Web API服务?我已经将我的WCF与Web API只是这样,我可以让我的应用程序J2ME有一个更简单的方式访问我的服务。问题是,我不知道如何调用从MIDlet我的Web API方法。你有没有做过类似的事情?你有任何联系,你可以分享?

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??

我已经找到了如何使用来自网络API的方法,但还是不知道怎么把我所得到的从Web API到的东西我真的可以显示手机屏幕上。

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

这是code我使用的是:

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();
    }
}

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

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>

现在我也看过,我应该使用kXML2,但所有的信息是如此混乱,唯一的好教程,我已经能够找到是的这个,问题在于它使用KXML,其中的根据这个页面是pcated德$ p $

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

请如果任何你曾经使用KXML2我真的AP preciate你能帮助我。

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

P.S。目前我的服务返回XML,但如果你知道如何使用JSON对象在Java ME的工作,我可以很容易地返回JSON来代替。

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.

先谢谢了。

推荐答案

我想大多数的JavaME开发人员通过调用一个网站的网址,像这样做只是。例如。

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

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

然后 yourwebservice.aspx 简单的返回数据。

对于我已经在工作的游戏排行榜系统,我称之为像

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

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

,它将输出以纯文本的高分,而我的MIDlet会再读取。

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

下面是读取返回值的一些例子:<一href=\"http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/io/HttpConnection.html\" rel=\"nofollow\">http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/io/HttpConnection.html

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

这篇关于如何使用来自一个Java ME应用程序的ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆