继续我们上个例子 , 完成J2ME端代码的编写.
我们的那个MIDlet继承了Runnable接口, 所以可以在程序中使用另外一个线程来访问WebService. (这点很关键, 有些手机在实现某些操作时就必须用多线程的方式来执行, 至少我的索爱是这样. 例如调用WMA来收发短信, 开启手机的摄像头等等...)
现在我们就来编写用来访问WebService的接口和实现类.
package com.joey.j2me.service; public interface UserService extends java.rmi.Remote { public java.lang.String request(java.lang.String username,java.lang.String password) throws java.rmi.RemoteException; }
一个非常简单的接口, 下面我们来创建它的实现类.
package com.joey.j2me.service.impl; import javax.microedition.xml.rpc.ComplexType; import javax.microedition.xml.rpc.Element; import javax.microedition.xml.rpc.Operation; import javax.microedition.xml.rpc.Type; import javax.xml.namespace.QName; import javax.xml.rpc.JAXRPCException; import com.joey.j2me.service.UserService; public class UserServiceBean implements UserService, javax.xml.rpc.Stub { protected static final QName _qname_UserName = new QName("", "username"); protected static final QName _qname_PassWord = new QName("", "password"); protected static final QName _qname_result = new QName("", "returnText"); protected static final QName _qname_request = new QName("http://cxf.joey.com/", "login"); protected static final QName _qname_requestResponse = new QName("http://cxf.joey.com/", "loginResponse"); protected static final QName _qname_wsdl_request = new QName("http://cxf.joey.com/", "login"); protected static final Element _login; protected static final Element _loginResponse; static { Element username = new Element(_qname_UserName, Type.STRING, 1, 1, true); Element password = new Element(_qname_PassWord, Type.STRING, 1, 1, true); ComplexType _complexType_request; _complexType_request = new ComplexType(); _complexType_request.elements = new Element[2]; _complexType_request.elements[0] = username; _complexType_request.elements[1] = password; _login = new Element(_qname_request, _complexType_request); Element _ReturnText = new Element(_qname_result, Type.STRING, 1, 1, true); ComplexType _complexType_requestResponse; _complexType_requestResponse = new ComplexType(); _complexType_requestResponse.elements = new Element[1]; _complexType_requestResponse.elements[0] = _ReturnText; _loginResponse = new Element(_qname_requestResponse, _complexType_requestResponse); } private String[] _propertyNames; private Object[] _propertyValues; public UserServiceBean () { _propertyNames = new String[] { ENDPOINT_ADDRESS_PROPERTY }; _propertyValues = new Object[] { "http://localhost:8080/UserManager" }; } public void _setProperty(String name, Object value) { System.out.println("_setProperty"); int size = _propertyNames.length; for (int i = 0; i < size; ++i) { if (_propertyNames[i].equals(name)) { _propertyValues[i] = value; return; } } String[] newPropNames = new String[size + 1]; System.arraycopy(_propertyNames, 0, newPropNames, 0, size); _propertyNames = newPropNames; Object[] newPropValues = new Object[size + 1]; System.arraycopy(_propertyValues, 0, newPropValues, 0, size); _propertyValues = newPropValues; _propertyNames[size] = name; _propertyValues[size] = value; } public Object _getProperty(String name) { System.out.println("_getProperty"); for (int i = 0; i < _propertyNames.length; ++i) { if (_propertyNames[i].equals(name)) { return _propertyValues[i]; } } if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) || PASSWORD_PROPERTY.equals(name)) { return null; } if (SESSION_MAINTAIN_PROPERTY.equals(name)) { return new java.lang.Boolean(false); } throw new JAXRPCException("Stub does not recognize property: " + name); } protected void _prepOperation(Operation op) { for (int i = 0; i < _propertyNames.length; ++i) { op.setProperty(_propertyNames[i], _propertyValues[i].toString()); } } public java.lang.String login(java.lang.String username,java.lang.String password) throws java.rmi.RemoteException { Object[] inputObject = new Object[2]; inputObject[0] = username; inputObject[1] = password; Operation op = Operation.newInstance(_qname_wsdl_request, _login, _loginResponse); _prepOperation(op); op.setProperty(Operation.SOAPACTION_URI_PROPERTY, ""); Object resultObj; try { resultObj = op.invoke(inputObject); } catch (JAXRPCException e) { Throwable cause = e.getLinkedCause(); if (cause instanceof java.rmi.RemoteException) { throw (java.rmi.RemoteException)cause; } throw e; } java.lang.String result; Object resultObj2 = ((Object[])resultObj)[0]; result = (java.lang.String)resultObj2; return result; } }
最后只要在MIdlet中的run方法里调用这个login方法就可以了.
未完...