2

Java ME アプリケーション (プロトタイプ) を作成しましたが、MIDlet から WEB API サービスを使用する必要があります。まず、MIDlet から Web API サービスを利用することは可能ですか?? J2ME アプリがより簡単な方法でサービスにアクセスできるようにするために、WCF を Web API に変換しました。問題は、MIDlet から Web API メソッドを呼び出す方法がわからないことです。似たようなことをしたことがありますか?共有できるリンクはありますか??

編集:

Web API からメソッドを使用する方法を見つけましたが、Web API から取得したものを実際にモバイル画面に表示できるものに変換する方法がまだわかりません。

これは私が使用しているコードです:

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

これは、GetCustomers から取得した XML の例です。

<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 を使用していることです。

KXML2 を使用したことがある方がいらっしゃいましたら、お役に立てれば幸いです。

PS現在、私のサービスは XML を返しますが、 Java ME で json オブジェクトを操作する方法を知っていれば、代わりに json を簡単に返すことができます。

前もって感謝します。

4

1 に答える 1