这是我的原始网络服务:
@Override
public boolean LogOn(String UserName, String Password) {
String username = "";
String password = "";
if (UserName != null) {
username = UserName;
}
if (Password != null) {
password = Password;
}
System.out.println("username is"+username);
System.out.println("password is"+password);
//test parameters
if (username.equals("admin") && password.equals("admin")) {
return true;
} else {
return false;
}
}
Android上的这个客户端:
private static final String METHOD_NAME = "LogOn";
private static final String NAMESPACE = "http://calculator.me.org/";
private static final String URL = "http://10.0.2.2:8080/logon/WebServiceImplService?wsdl";
@Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Add the input variables for the method
request.addProperty("username", "admin");
request.addProperty("password","admin");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
System.setProperty("http.keepAlive", "false");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
androidHttpTransport.call(getSoapAction(METHOD_NAME), envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
//get response from web server
String d =result.getProperty("return").toString();
// Logging the raw request and response (for debugging purposes)
Log.d(TAG, "HTTP REQUEST:\n" + androidHttpTransport.requestDump);
Log.d(TAG, "HTTP RESPONSE:\n" + androidHttpTransport.responseDump);
Log.i(TAG,"response from service:"+d);
if(d.equals("true"))
{
//The result will be here, use it wisely :)
return 1;
}
else
return 0;
} catch (Exception e) {
Log.e(TAG,"doInBackground failed"+e);
e.printStackTrace();
return 0;
}
}
当我手动测试网络服务时,记录网络服务:
INFO: username isadmin
INFO: password isadmin
当我尝试从 android 客户端登录时,我得到下一个日志:
INFO: username is
INFO: password is
如果我要删除 nullpointer 检查,在 web 服务上我会得到 NullPointerException。来自 Android 客户端的日志:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): HTTP REQUEST:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><n0:LogOn id="o0" c:root="1" xmlns:n0="http://calculator.me.org/"><username i:type="d:string">admin</username><password i:type="d:string">admin</password></n0:LogOn></v:Body></v:Envelope>
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): HTTP RESPONSE:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): <?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:LogOnResponse xmlns:ns2="http://calculator.me.org/"><return>false</return></ns2:LogOnResponse></S:Body></S:Envelope>
11-12 02:29:55.081: I/SmsSender:LogOnAsync(1878): response from service:false
怎么了?