如何从与KSOAP2和Android web服务免费获赠消息获赠、消息、Android、web

2023-09-04 23:22:42 作者:熊扑奶团

我试图做一个简单的ksoap2教程。这是

在链接

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

我怎么也得使用SoapObject?

这是我的code:

 公共类WS_Auth_ComplexObjectsActivity延伸活动{
    / **第一次创建活动时调用。 * /

    字符串NAMESPACE =htt​​p://WS.androidroleplay.fk4.de.hs_bremen.de;
    字符串METHOD_NAME =GetSumOfTwoInts;
    字符串SOAP_ACTION =htt​​p://WS.androidroleplay.fk4.de.hs_bremen.de/GetSumOfTwoInts;
    字符串的URL =htt​​p://192.168.178.28:8080/WebProject_DB16/services/HelloWorldWS?wsdl;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        SoapObject请求=新SoapObject(命名空间METHOD_NAME);

        的PropertyInfo PI =新的PropertyInfo();
        pi.setName(操作数);
        pi.setValue(2);
        pi.setType(int.class);
        Request.addProperty(PI);

        的PropertyInfo PI2 =新的PropertyInfo();
        pi2.setName(和Operand2);
        pi2.setValue(5);
        pi2.setType(int.class);
        Request.addProperty(PI2);

        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(请求);

// envelope.addMapping(命名空间中,整数,);
        HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);

        尝试
        {
             //没有得票数folgt死Authentifikation MIT数字高程模型用户管理UND Passwort管理
            名单< HeaderProperty> headerList =新的ArrayList< HeaderProperty>();
            headerList.add(新HeaderProperty(授权,基本+ org.kobjects.base64.Base64.en code(雄猫:雄猫.getBytes()))); // 用户名密码

            androidHttpTransport.call(SOAP_ACTION,信封,headerList);
            androidHttpTransport.debug = TRUE;

            SoapObject响应=(SoapObject)envelope.getResponse();
            字符串结果= response.getAttributeAsString(0);

            TextView的电视=(TextView中)findViewById(R.id.TextView);
            tv.setText(文字:​​+结果); //的CategoryId:+ C.getCategoryId()+姓名:+ C.getName()+描述:+ C.getDescription()
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
    }
}
 

和本我的web服务:

 公共类HelloWorldWS
{

    公众诠释GetSumOfTwoInts(INT操作数,整数和Operand2)
    {
        的System.out.println(操作数+++和Operand2 +=+(操作数+ Operand2中));

       返回操作数1 + Operand2中;
    }


}
 
ksoap2 android

解决方案

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

  SoapPrimitive响应=(SoapPrimitive)envelope.getResponse();
 

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

  SoapObject响应=(SoapObject)envelope.getResponse();
 

和解析它。

i tried to do a simple ksoap2 tutorial. This is the link

My Problem is, I only get a response, if I use SoapPrimitive and Ksoap...2.6.4.jar with Ksoap...2.4.jar and or "SoapObject response = (SoapObject)envelope.getResponse();" I've got an exception.

How i have to use the SoapObject?

This is my Code:

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();
        }
    }
}

And this my Webservice:

public class HelloWorldWS
{   

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

       return Operand1 + Operand2;
    }


}

解决方案

Your service method returns only a integer value. In ksoap2, if webservice response has only one value(integer, string..) you should use

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

and response.toString(); is result that you want. If your response has complex types (Objects), you should use

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

and parse it.