0

这是我的原始网络服务:

@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

怎么了?

4

1 回答 1

2

我有一个类似的问题。我决定阅读:

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 时,这才会有所帮助

于 2013-11-12T08:25:45.003 回答