soap webservice方法,从android调用一个对象

时间:2016-05-11 14:39:32

标签: java android

我需要从我的android app调用soap webservice.webservice方法有一个有4个值的对象。我读过关于ksoap lib.But我很困惑服务中的对象如何作为参数从android.Please发送帮我解决这个问题。

<CKError 0x125647120: "Not Authenticated" (9/1002); "CloudKit access was denied by user settings"; Retry after 3.0 seconds>

这是怎么做的? 我没有使用过ksoap和soap webservices.Please help

1 个答案:

答案 0 :(得分:0)

假设您有像这样定义的webmethod,它返回Category对象。

KvmSerializable

Say Category类有三个属性,如CategoryId,Name和Description.So要与KSoap一起使用,你必须实现package CommonObjects; import java.util.Hashtable; import org.ksoap2.serialization.KvmSerializable; import org.ksoap2.serialization.PropertyInfo; public class Category implements KvmSerializable { public int CategoryId; public String Name; public String Description; public Category(){} public Category(int categoryId, String name, String description) { CategoryId = categoryId; Name = name; Description = description; } public Object getProperty(int arg0) { switch(arg0) { case 0: return CategoryId; case 1: return Name; case 2: return Description; } return null; } public int getPropertyCount() { return 3; } public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch(index) { case 0: info.type = PropertyInfo.INTEGER_CLASS; info.name = "CategoryId"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "Name"; break; case 2: info.type = PropertyInfo.STRING_CLASS; info.name = "Description"; break; default:break; } } public void setProperty(int index, Object value) { switch(index) { case 0: CategoryId = Integer.parseInt(value.toString()); break; case 1: Name = value.toString(); break; case 2: Description = value.toString(); break; default: break; } } }

public void WebServiceCallExample()
    {
        String NAMESPACE = "http://vladozver.org/";
        String METHOD_NAME = "GetCategoryById";
        String SOAP_ACTION = "http://vladozver.org/GetCategoryById";
        String URL = "http://192.168.1.3/VipEvents/Services/CategoryServices.asmx";

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        /*
         * Create Category with Id to be passed as an argument
         * 
         * */
        Category C = new Category();
        C.CategoryId = 1;

        /*
         * Set the category to be the argument of the web service method
         * 
         * */
        PropertyInfo pi = new PropertyInfo();
        pi.setName("C");
        pi.setValue(C);
        pi.setType(C.getClass());
        Request.addProperty(pi);

        /*
         * Set the web service envelope
         * 
         * */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        /*
         * Call the web service and retrieve result ... how luvly <3
         * 
         * */
        try
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject)envelope.getResponse();
            C.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
            C.Name =  response.getProperty(1).toString();
            C.Description = (String) response.getProperty(2).toString();
            TextView tv = (TextView)findViewById(R.id.TextView01);
            tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

现在你必须映射属性,阅读它们并写下它们。

因此,Web服务调用是:

PropertyInfo pi = new PropertyInfo();
pi.setName("Category");
pi.setValue(C);
pi.setType(C.getClass());
Request.addProperty(pi);

要使用此代码,请确保使用您自己的代码替换此方法中的前三个变量。注意:SOAP_ACTION实际上可以计算为NAMESPACE + METHOD_NAME

在关于属性信息的行中,我将展示如何将Category类复杂对象实际传递给KSOAP。

envelope.addMapping(NAMESPACE, "Category",new Category().getClass());

关于返回类型,如果您的Web方法返回一个复杂对象(例如我们的),您需要告诉KSOAP如何处理响应。这是通过以下代码完成的:

androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
C.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
C.Name =  response.getProperty(1).toString();
C.Description = (String) response.getProperty(2).toString();

这部分代码是响应的实际检索:

change

blogpost的参考资料。