Webservice------(3)--------ksoap 2リモートアクセス


1、j 2 meの開発環境については、詳しくは述べない
2、新しいmidlet、コードは以下の通り
パラメータを持たないwebserviceのテスト:サービスはaxis 2公式のサービスです

	import java.io.IOException;   
	  
	import javax.microedition.midlet.*;   
	import javax.microedition.io.ConnectionNotFoundException;   
	import javax.microedition.lcdui.*;   
	  
	import org.ksoap2.*;   
	import org.ksoap2.serialization.*;   
	import org.ksoap2.transport.*;   
	import org.xmlpull.v1.XmlPullParserException;   
	  
	  
	  
	public class Test extends MIDlet implements CommandListener{   
	  
	    private Display dis;   
	    private Command connectCmd=new Command("  ",Command.OK,1);   
	    private Command exitCmd=new Command("  ",Command.EXIT,1);   
	    private Form f;   
	    private StringItem si=new StringItem("","");   
	       
	    public Test() {   
	        // TODO Auto-generated constructor stub   
	    }   
	  
	    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {   
	        // TODO Auto-generated method stub   
	  
	    }   
	  
	    protected void pauseApp() {   
	        // TODO Auto-generated method stub   
	  
	    }   
	  
	    protected void startApp() throws MIDletStateChangeException {   
	        // TODO Auto-generated method stub   
	        dis=Display.getDisplay(this);   
        f=new Form("  Web Service");   
	        f.append("      :"+"
"); f.addCommand(connectCmd); f.addCommand(exitCmd); f.setCommandListener(this); dis.setCurrent(f); } public void commandAction(Command c, Displayable s) { // TODO Auto-generated method stub if (c == exitCmd) { try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(c==connectCmd) { // Thread, ksoap2 Thread webserviceThread=new Thread() { public void run() { String methodName="getVersion"; String serviceURL="http://localhost:8080/axis2/services/Version"; SoapObject request=new SoapObject(null,methodName); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut=request; HttpTransport ht=new HttpTransport(serviceURL); ht.debug=true; String Response = null; try { ht.call("urn:fengxing/Hello", envelope);// .NET SoapRpcMethod Action Response = envelope.getResponse().toString(); }catch (ConnectionNotFoundException e) { System.out.println(" "); return; } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } System.out.println("dump>>" + ht.responseDump); si.setText(Response); f.append(si); f.removeCommand(connectCmd); } }; webserviceThread.start(); } } }

3、パラメータ付きwebserviceのテスト

package com.database.client;

import java.io.IOException;

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

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.xmlpull.v1.XmlPullParserException;

public class Test2 extends MIDlet implements CommandListener {

	private Display dis;
	private Command connectCmd = new Command("  ", Command.OK, 1);
	private Command exitCmd = new Command("  ", Command.EXIT, 1);
	private Form f;
	private StringItem si = new StringItem("", "");

	public Test2() {
	}

	protected void destroyApp(boolean arg0) throws IDletStateChangeException 

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		dis = Display.getDisplay(this);
		f = new Form("  Web Service");
		f.append("      :" + "
"); f.addCommand(connectCmd); f.addCommand(exitCmd); f.setCommandListener(this); dis.setCurrent(f); } public void commandAction(Command c, Displayable s) { if (c == exitCmd) { try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException e) { e.printStackTrace(); } } if (c == connectCmd) { // Thread, ksoap2 Thread webserviceThread = new Thread() { public void run() { String serviceNamespace = "http://calculator"; String methodName = "plus"; String serviceURL = "http://localhost:8090/WebServiceProject/services/CalculateService"; SoapObject request = new SoapObject(serviceNamespace, methodName); request.addProperty("x", "abc"); request.addProperty("y", "abc"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; HttpTransport ht = new HttpTransport(serviceURL); ht.debug = true; String Response = null; try { ht.call(null, envelope);// .NET SoapRpcMethod Action Response = envelope.getResponse().toString(); } catch (ConnectionNotFoundException e) { System.out.println(" "); return; } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } System.out.println("dump>>" + ht.responseDump); si.setText(Response); f.append(si); f.removeCommand(connectCmd); } }; webserviceThread.start(); } } }