domingo, 18 de abril de 2010

JME, WEB SERVICES Y Xparse

Como crear una aplicacion JME, con conexion a un Web Service con Netbeans

Abrimos Netbeans



Bajo la categoria JavaME, creamos un Mobile Application:

Que la llamaremos ExampleWS




Seleccionamos CLDC-1.1. y MIDP-2.0


Bajo MIDP seleccionamos Java ME Web Service Client

http://www.webservicex.net/globalweather.asmx





Damos click bajo Retrieve WSDL y luego click en Finish. Luego, click sobre generate Stubs

Regresamos a HelloMIDlet y abrimos form(dando click sobre el cuadrado rojo en el flow)

Arrastramos un nuevo okCommand, sobre el comando damos click sobre goto source y además creamos el siguiente interface gráfico:

Creamos una libreria en Netbeans, en este caso un parser XML, como por ejemplo xparse-j-1.1.jar, que se lo puede descargar de:

http://prdownloads.sourceforge.net/exploringxml/xparse-j-1.1.jar


y luego de "write post-action user code here", del boton OK, ingresamos el siguiente código:
(Este código tenemos que ponerlo dentro de un nuevo Thread para evitar un posible deadlock)

Thread t=new Thread(){

public void run(){

GlobalWeather gw=new GlobalWeather_Stub();

try {

String xml = gw.getWeather(textField.getString(), textField1.getString());

Node n=new Xparse().parse(xml);

int occurs[]={1};

Node weather=n.find("CurrentWeather", occurs);

stringItem.setText(weather.find("Temperature", occurs).getCharacters());

stringItem1.setText(weather.find("Pressure", occurs).getCharacters());

stringItem2.setText(weather.find("Wind", occurs).getCharacters());

} catch (RemoteException ex) {

ex.printStackTrace();

}

}

};

t.start();





No hay comentarios:

Publicar un comentario