webservice -------(3)-----------ksoap 2 远程访问

1、j2me 的开发环境,不再详述

2、新建midlet,代码如下

测试不带参数的webservice:服务是axis2官方的service
	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("要显示的内容:"+"\n");   
	        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("要显示的内容:" + "\n");
		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();

		}

	}
}


你可能感兴趣的:(thread,C++,c,webservice,F#)