Implementando webservices em dispositivos J2ME

JSR 172, Web Services API, extend a plataforma J2ME para suportar web services. Esta webservice api consiste em 2 pacotes opcionais: “Java API for Remote Method Invocations” e “Java API for XML Processing”.

Abaixo está uma ilustração simples do webservice usando parsers diferentes do xml.

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import org.ksoap.*;
import org.kobjects.serialization.*;
import org.kxml.*;
import org.kxml.io.*;
import org.kxml.parser.*;

public class HelloWorld extends MIDlet implements CommandListener {
    
    private Display display;
    private Command doneCommand;
    private TextField textField;
    
    public HelloWorld() throws Exception {
        display = Display.getDisplay(this);
        doneCommand = new Command("DONE", Command.CANCEL, 1);
    }
    
    private String soapMesg =
            "<SOAP-ENV:Envelope " +
            "xmlns:SOAP-ENV=\"http://www.w3.org/2001/12/soap-envelope\" " +
            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
            "<SOAP-ENV:Body>" +
            "<message xsi:type=\"xsd:string\">Hello World</message>" +
            "<message02 xsi:type=\"xsd:string\">unreachable</message02>" +
            "</SOAP-ENV:Body>" +
            "</SOAP-ENV:Envelope>";
    
    private String soapRespMesg =
            "<SOAP-ENV:Envelope " +
            "xmlns:SOAP-ENV=\"http://www.w3.org/2001/12/soap-envelope\" " +
            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
            "<SOAP-ENV:Body>" +
            "<result>" +
            "<message xsi:type=\"xsd:string\">Hello World</message>" +
            "</result>" +
            "</SOAP-ENV:Body>" +
            "</SOAP-ENV:Envelope>";
    
    private String processMesg(String mesg) throws Exception {
        ByteArrayInputStream bis = new ByteArrayInputStream(mesg.getBytes());
        InputStreamReader reader = new InputStreamReader(bis);
        XmlParser xp = new XmlParser(reader);
        
        SoapEnvelope envelope = new SoapEnvelope(new ClassMap(Soap.VER12));
        envelope.parse(xp);
        
        return (String) envelope.getBody();
    }
    
    private String processRespMesg(String mesg) throws Exception {
        ByteArrayInputStream bis = new ByteArrayInputStream(mesg.getBytes());
        InputStreamReader reader = new InputStreamReader(bis);
        XmlParser xp = new XmlParser(reader);
        
        SoapEnvelope envelope = new SoapEnvelope(new ClassMap(Soap.VER12));
        envelope.parse(xp);
        
        return (String) envelope.getResult();
    }
    
    public void startApp() {
        String mesg;
        Form form = new Form("Exemplo Hello World");
        try {
            mesg = processMesg(soapMesg);
        catch (Exception e) {
            mesg = "falha no parsing";
            e.printStackTrace();
        }
        form.append(new StringItem("SoapMesg", mesg));
        try {
            mesg = processRespMesg(soapRespMesg);
        catch (Exception e) {
            mesg = "falha no parsing";
            e.printStackTrace();
        }
        form.append(new StringItem("SoapRespMesg", mesg));
        
        form.addCommand(doneCommand);
        form.setCommandListener((CommandListener) this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command command, Displayable screen) {
        if (command == doneCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}