How to create SOAP request via ksoap2

て烟熏妆下的殇ゞ 提交于 2019-11-28 00:36:30
shadesco

Ok so you have a function FReadStatus:

<message name="FReadStatus">
  <part name="parameters" element="tns:FReadStatus"/>
</message>

which has an element FReadStatus of complex type(ie object, which is a class found on the server).
This complex type has an attribute arg0 of type String. Its definition is:

<xs:complexType name="FReadStatus">
  <xs:sequence>
    <xs:element name="arg0" type="xs:string" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

So you will need to create a local class that implements kvmSerializable to map this complex type to its corresponding class on the server, so you will do :

public class FReadStatus implements KvmSerializable {

String mac; 

@Override
public Object getProperty(int arg0) {
switch (arg0){
    case 0:
        return mac;
    default:
        return null;
        }
}

@Override
public int getPropertyCount() {
    return 1;//because you have 1 parameter
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0)
{

    case 0:
        arg2.type = PropertyInfo.STRING_CLASS;//because its type is string
        arg2.name = "arg0";
        break;
    default:break;
}

}

@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0)
{
    case 0:
        mac=  (String)arg1;
        break;
    default:
        break;
}
}

Now that you have the class , you will do the following in the code you had:

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    PropertyInfo pi = new PropertyInfo();
    pi.setName("arg0");
    pi.setValue("000D6F0000");
    pi.setType(FReadStatus .class);
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request);

     //Now you have to add mapping to map the local class created, to the one on the server
    envelope.addMapping(NAMESPACE , FReadStatus.class.getSimpleName(), FReadStatus .class);

    // Add marshalling (this one might not be necessary, but ill just add it)
    Marshal floatMarshal = new MarshalFloat();
    floatMarshal.register(envelope);

    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);//AndroidHttpTransport INSTEAD OF HttpTransportSE 

    androidHttpTransport.debug = true;//NEW ADDED
    try {           

        androidHttpTransport.call(SOAP_ACTION, envelope);

       //Important Outputs to check how the request/Response looks like.. Check them in Logcat to find these outputs
       System.out.println("requestDump is :"+androidHttpTransport.requestDump);
       System.out.println("responseDump is :"+androidHttpTransport.responseDump);
       System.out.println("response"+envelope.getResponse());

    } catch (Exception e){}

Let me know wt happens. You must use Logcat to check requestDump and responseDump

UPDATE: answering your question about UnknowHostException
possible causes and solutions

  • Check if in your AndroidManifest.xml you have :

    <uses-permission android:name="android.permission.INTERNET" />
    
  • If you are using an emulator do as mentioned in this link

  • If you are behind a proxy do :

    System.setProperty("http.proxyHost", "my.proxyhost.com");
    System.setProperty("http.proxyPort", "1234");
    
  • You might need to use warmup the dns , check this link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!