Невозможно войти через KSOAP Android на веб-сервис
Это мой примитивный веб-сервис:
@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
Если я удаляю проверку нулевого указателя, на веб-сервисе я получаю исключение 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
В чем дело?
1 ответ
Решение
У меня похожая проблема. Я решил прочитать:
Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi1 = new PropertyInfo();
pi1.setName("arg0");
pi1.setValue("admin");
pi1.setType(String.class);
request.addProperty(pi1);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("arg1");
pi2.setValue("admin");
pi2.setType(String.class);
request.addProperty(pi2);
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;
}
}
Это поможет, только если вы привели весь метод LogOn