0

我试着做一个简单的 ksoap2 教程。这是链接

我的问题是,如果我将 SoapPrimitive 和 Ksoap...2.6.4.jar 与 Ksoap...2.4.jar 和或“SoapObject response = (SoapObject)envelope.getResponse();”一起使用,我只会得到响应 我有一个例外。

我必须如何使用 SoapObject?

这是我的代码:

public class WS_Auth_ComplexObjectsActivity extends Activity {
    /** Called when the activity is first created. */

    String NAMESPACE = "http://WS.androidroleplay.fk4.de.hs_bremen.de";
    String METHOD_NAME = "GetSumOfTwoInts";
    String SOAP_ACTION = "http://WS.androidroleplay.fk4.de.hs_bremen.de/GetSumOfTwoInts";
    String URL = "http://192.168.178.28:8080/WebProject_DB16/services/HelloWorldWS?wsdl";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("Operand1");
        pi.setValue(2);
        pi.setType(int.class);
        Request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("Operand2");
        pi2.setValue(5);
        pi2.setType(int.class);
        Request.addProperty(pi2);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(Request);

//        envelope.addMapping(NAMESPACE, "Integer",);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try
        {
             // Hier folgt die Authentifikation mit dem User Admin und Passwort Admin
            List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
            headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("tomcat:tomcat".getBytes()))); // "username:password"

            androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
            androidHttpTransport.debug=true;

            SoapObject response = (SoapObject)envelope.getResponse();
            String result =  response.getAttributeAsString(0);

            TextView tv = (TextView)findViewById(R.id.TextView);
            tv.setText("Text: " + result); //"CategoryId: " +C.getCategoryId() + " Name: " + C.getName() + " Description: " + C.getDescription()
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是我的网络服务:

public class HelloWorldWS
{   

    public int GetSumOfTwoInts(int Operand1, int Operand2 )
    {
        System.out.println(Operand1+ " + "+ Operand2 +" = "+ (Operand1 + Operand2));

       return Operand1 + Operand2;
    }


}
4

3 回答 3

0

试试这个

SoapObject resultObj = (SoapObject) envelope.bodyIn;
String result = (String)resultObj.getProperty(0);
于 2012-06-14T17:02:55.583 回答
0

您的服务方法只返回一个整数值。在 ksoap2 中,如果 web 服务响应只有一个值(整数、字符串..),您应该使用

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

response.toString();是你想要的结果。如果您的响应具有复杂类型(对象),您应该使用

SoapObject response = (SoapObject) envelope.getResponse();

并解析它。

于 2012-06-15T06:53:06.813 回答
0

试试这个

String result = envelope.getResponse().toString();
return result;
于 2013-09-24T10:16:45.373 回答