java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Implementing webservices from J2ME devices E-mail
User Rating: / 6
PoorBest 

JSR 172,Web Services API , extends the J2ME platform to support web services. This webservice api consist of 2 optional packages: "Java API for Remote Method Invocations" and "Java API for XML Processing".

Below is a simple illustration of webservice using differnt xml parsers.

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 mesgthrows 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 (Stringenvelope.getBody();
    }
    
    private String processRespMesg(String mesgthrows 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 (Stringenvelope.getResult();
    }
    
    public void startApp() {
        String mesg;
        Form form = new Form("Hello World Example");
        try {
            mesg = processMesg(soapMesg);
        catch (Exception e) {
            mesg = "parsing failed";
            e.printStackTrace();
        }
        form.append(new StringItem("SoapMesg", mesg));
        try {
            mesg = processRespMesg(soapRespMesg);
        catch (Exception e) {
            mesg = "parsing failed";
            e.printStackTrace();
        }
        form.append(new StringItem("SoapRespMesg", mesg));
        
        form.addCommand(doneCommand);
        form.setCommandListener((CommandListenerthis);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command command, Displayable screen) {
        if (command == doneCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.