我有一个简单的 java 项目,其中包含一个简单的 web 服务
包:pcp.webservices
文件 WEB-INF/lib/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Pcp web services</display-name>
<servlet>
<servlet-name>pcpWS</servlet-name>
<servlet-class>pcp.webservices.pcpWS</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pcpWS</servlet-name>
<url-pattern>/pcpWS</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
文件src/pcpWS.java:
@WebService(name = "pcpWS")
/** @SOAPBinding indica información vinculate para los mensajes SOAP.*/
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL,
parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class pcpWS {
@WebMethod
// Usuario is an external class containing filed likes Name,Surname,...
public Usuario ValidarUsuario(@WebParam(name = "login") String login,@WebParam(name = "password") String password) {
//comprobar si existe o no el usuario
//codigo de prueba
Usuario u=new Usuario();
if (login.contentEquals("H"))
{
u.IdUsuario = "8888";
u.Nombre = "Andrea";
u.Apellidos = "Martín";
u.Rol = "Administrador";
u.Centro = "Keyland Valladolid";
}
else
{
u.IdUsuario = "9999";
u.Nombre = "Pablo";
u.Apellidos = "Díaz";
u.Rol = "Trabajador";
u.Centro = "Keyland Valladolid";
}
return u;
}
}
webservice 部署 JBoss 4.2 并生成以下wsdl 文件:
<definitions name='pcpWSService' targetNamespace='http://webservices.pcp/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://webservices.pcp/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<types>
<xs:schema targetNamespace='http://webservices.pcp/' version='1.0' xmlns:tns='http://webservices.pcp/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='ConsultarNotificaciones' type='tns:ConsultarNotificaciones'/>
<xs:element name='ConsultarNotificacionesResponse' type='tns:ConsultarNotificacionesResponse'/>
<xs:element name='Responsables' type='tns:Responsables'/>
<xs:element name='ResponsablesResponse' type='tns:ResponsablesResponse'/>
<xs:element name='ValidarUsuario' type='tns:ValidarUsuario'/>
<xs:element name='ValidarUsuarioResponse' type='tns:ValidarUsuarioResponse'/>
<xs:complexType name='ValidarUsuario'>
<xs:sequence>
<xs:element minOccurs='0' name='login' type='xs:string'/>
<xs:element minOccurs='0' name='password' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='ValidarUsuarioResponse'>
<xs:sequence>
<xs:element minOccurs='0' name='return' type='tns:usuario'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='usuario'>
<xs:sequence>
<xs:element minOccurs='0' name='IdUsuario' type='xs:string'/>
<xs:element minOccurs='0' name='Nombre' type='xs:string'/>
<xs:element minOccurs='0' name='Apellidos' type='xs:string'/>
<xs:element minOccurs='0' name='Rol' type='xs:string'/>
<xs:element minOccurs='0' name='Centro' type='xs:string'/>
<xs:element minOccurs='0' name='apellidos' type='xs:string'/>
<xs:element minOccurs='0' name='centro' type='xs:string'/>
<xs:element minOccurs='0' name='idUsuario' type='xs:string'/>
<xs:element minOccurs='0' name='nombre' type='xs:string'/>
<xs:element minOccurs='0' name='rol' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='Responsables'>
<xs:sequence/>
</xs:complexType>
<xs:complexType name='ResponsablesResponse'>
<xs:sequence>
<xs:element maxOccurs='unbounded' minOccurs='0' name='return' type='tns:responsable'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='responsable'>
<xs:sequence>
<xs:element minOccurs='0' name='Id' type='xs:string'/>
<xs:element minOccurs='0' name='Nombre' type='xs:string'/>
<xs:element minOccurs='0' name='nombre' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='ConsultarNotificaciones'>
<xs:sequence>
<xs:element minOccurs='0' name='idUsu' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='ConsultarNotificacionesResponse'>
<xs:sequence>
<xs:element maxOccurs='unbounded' minOccurs='0' name='return' type='tns:notificacion'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='notificacion'>
<xs:sequence>
<xs:element minOccurs='0' name='Descripcion' type='xs:string'/>
<xs:element minOccurs='0' name='Fecha' type='xs:string'/>
<xs:element minOccurs='0' name='Tipo' type='xs:string'/>
<xs:element minOccurs='0' name='URL' type='xs:string'/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</types>
<message name='pcpWS_Responsables'>
<part element='tns:Responsables' name='Responsables'></part>
</message>
<message name='pcpWS_ValidarUsuarioResponse'>
<part element='tns:ValidarUsuarioResponse' name='ValidarUsuarioResponse'></part>
</message>
<message name='pcpWS_ResponsablesResponse'>
<part element='tns:ResponsablesResponse' name='ResponsablesResponse'></part>
</message>
<message name='pcpWS_ConsultarNotificaciones'>
<part element='tns:ConsultarNotificaciones' name='ConsultarNotificaciones'></part>
</message>
<message name='pcpWS_ConsultarNotificacionesResponse'>
<part element='tns:ConsultarNotificacionesResponse' name='ConsultarNotificacionesResponse'></part>
</message>
<message name='pcpWS_ValidarUsuario'>
<part element='tns:ValidarUsuario' name='ValidarUsuario'></part>
</message>
<portType name='pcpWS'>
<operation name='ConsultarNotificaciones' parameterOrder='ConsultarNotificaciones'>
<input message='tns:pcpWS_ConsultarNotificaciones'></input>
<output message='tns:pcpWS_ConsultarNotificacionesResponse'></output>
</operation>
<operation name='Responsables' parameterOrder='Responsables'>
<input message='tns:pcpWS_Responsables'></input>
<output message='tns:pcpWS_ResponsablesResponse'></output>
</operation>
<operation name='ValidarUsuario' parameterOrder='ValidarUsuario'>
<input message='tns:pcpWS_ValidarUsuario'></input>
<output message='tns:pcpWS_ValidarUsuarioResponse'></output>
</operation>
</portType>
<binding name='pcpWSBinding' type='tns:pcpWS'>
<soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='ConsultarNotificaciones'>
<soap:operation soapAction=''/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
<operation name='Responsables'>
<soap:operation soapAction=''/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
<operation name='ValidarUsuario'>
<soap:operation soapAction=''/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
</binding>
<service name='pcpWSService'>
<port binding='tns:pcpWSBinding' name='pcpWSPort'>
<soap:address location='http://127.0.0.1:8080/webServices/pcpWS'/>
</port>
</service>
</definitions>
调用代码(来自android应用程序):
private final String NAMESPACE = "http://webservices.pcp/";
private final String URL = "http://127.0.0.1";
private final String SOAPACTION = "";
private final String METHOD = "ValidarUsuario";
String user = pars[0];
String password = pars[1];
SoapObject request = new SoapObject(NAMESPACE, METHOD);
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sobre.dotNet = false;
request.addProperty("login", user);
request.addProperty("password", password);
sobre.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try {
transporte.call(SOAPACTION, sobre);
} catch (IOException e) {
// JOptionPane.e.printStackTrace();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SoapObject resultado = null;
try {
resultado = (SoapObject)sobre.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//comprobar si devuelve un usuario nulo o no
Usuario usu = new Usuario();
usu.IdUsuario= resultado.getProperty(0).toString();
usu.Nombre = resultado.getProperty(1).toString();
usu.Apellidos = resultado.getProperty(2).toString();
usu.Rol = resultado.getProperty(3).toString();
usu.Centro=resultado.getProperty(4).toString();
启动的异常是XmlPullParserException,实际上捕获异常后的变量resultado仍然为空。
似乎正确调用了 Web 服务,但启动了异常。
难道我做错了什么?
这是完整的logcat 堆栈跟踪:
06-18 11:49:21.964: E/AndroidRuntime(335): FATAL EXCEPTION: main
06-18 11:49:21.964: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{es.es/es.es.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{es.es/es.es.MiCuentaActivity}: java.lang.NullPointerException
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-18 11:49:21.964: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 11:49:21.964: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
06-18 11:49:21.964: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-18 11:49:21.964: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-18 11:49:21.964: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
06-18 11:49:21.964: E/AndroidRuntime(335): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{es.es/es.es.MiCuentaActivity}: java.lang.NullPointerException
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
06-18 11:49:21.964: E/AndroidRuntime(335): at es.es.MainActivity.onCreate(MainActivity.java:31)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-18 11:49:21.964: E/AndroidRuntime(335): ... 11 more
06-18 11:49:21.964: E/AndroidRuntime(335): Caused by: java.lang.NullPointerException
06-18 11:49:21.964: E/AndroidRuntime(335): at es.es.MiCuentaActivity.onCreate(MiCuentaActivity.java:25)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 11:49:21.964: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-18 11:49:21.964: E/AndroidRuntime(335): ... 19 more
(MainActivity 是应用程序中的“基础”活动,代码在 AsynkThread 中运行,因为如果在主线程中完成,AccesoCuentaActivity,它会启动NetworkOnMainThreadException
)