如何从SOAP调用数据?

时间:2016-08-17 22:09:40

标签: android soap

我正在使用Android Studio中的SOAP项目。我是SOAP的新手,我研究了一些文章。我即将解决SOAP但我仍然坚持一些事情。我有一个XML结构,我无法从这个XML标签获取数据。实际上我对以“tem:...”开头的标签一无所知。

  

SOAP XML标记

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetForexStocksandIndexesInfo>
         <tem:request>
            <tem:IsIPAD>true</tem:IsIPAD>
            <tem:DeviceID>test</tem:DeviceID>
            <tem:DeviceType>ipad</tem:DeviceType>
            <tem:RequestKey>%%UmVxdWVzdElzVmFsaWQxNjowNToyMDEyIDExOjU0%%</tem:RequestKey>
        <tem:Period>Day</tem:Period>
         </tem:request>
      </tem:GetForexStocksandIndexesInfo>
   </soapenv:Body>
</soapenv:Envelope>
  

我的类使用数据

public void CallWebService() {
        final Date date = new Date();
        final String getCurrentDate = new SimpleDateFormat("dd:MM:yyyy HH:mm", Locale.ROOT).format(date);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                key = encryptManager.PushData("RequestIsValid" + getCurrentDate);
                Log.i("Key: ", key);

                key2 = sharesandIndexesListManager.PushData2("true","test","ipad",key,"Day");
                Log.i("Key2: ", key2);
            }
        });

thread.start();     }

我在日志中看到“key”var因为我看到了web url中变量的类型,但我没有看到变量的类型,例如web url中的tem:DeviceType-。另外Key2返回null。请帮帮我:)谢谢。

错误:org.xmlpull.v1.XmlPullParserException:意外类型(位置:END_DOCUMENT null @ 1:1 in java.io.InputStreamReader@21da6108)

1 个答案:

答案 0 :(得分:0)

我建议使用kso​​ap2-android库进行解析。如果您不知道值的类型,只需在SoapObject上使用getPropertyAsString即可。

试试这个: SoapObject messageObject =(SoapObject)response.getProperty(&#34; request&#34;);

//从上面提取最后一个 String lastAsString = messageObject.getPropertyAsString(&#34; DeviceID&#34;);

在这里查看SoapObject方法 http://kobjects.org/ksoap2/doc/api/

如果有帮助,请告诉我。

UPD

public class DeviceDetail implements KvmSerializable {

    public String IsIPAD = null;
    public String DeviceID= null;
   //TODO: ADD MORE VALUES YOURSELF! LIKE DeviceType

    private final String DATA_NAMESPACE = "http://schemas. YOUR SCHEMA HERE";

    public DeviceDetail () {
    }

    @Override
    public Object getProperty(int arg0) {

        switch (arg0) {
        case 0:
            return this.IsIPAD;
        case 1:
            return this.DeviceID;
        }

        return null;
    }

    @Override
    public int getPropertyCount() {

  //TODO: change to number of items accordingly
        return 2;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {

        arg2.namespace = DATA_NAMESPACE;

        switch (arg0) {
        case 0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "IsIPAD";
            break;
        case 1:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "DeviceID";
            break;
        default:
            break;
        }

    }

    @Override
    public void setProperty(int arg0, Object arg1) {

        switch (arg0) {
        case 0:
            this.IsIPAD= arg1.toString();
            break;
        case 1:
            this.DeviceID= arg1.toString();
            break;
        default:
            break;
        }

    }

   }

首先根据您的需求在服务器上创建请求:

//TODO: change to appropriate values
 String METHOD_NAME = "MyMethod";
 String INTERFACE = "IMyInterface";
 String NAMESPACE = "http://tempuri.org/";
 String SOAP_ACTION = NAMESPACE + INTERFACE + "/" + METHOD_NAME; 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("APIKey", API_KEY);
request.addProperty("AuthToken",  AuthToken);
request.addProperty("UserID", 1);

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

然后你可以执行调用并解析你的肥皂数据:

  HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); 
  androidHttpTransport.call(SOAP_ACTION, envelope);// call
  DeviceDetail responseObject = (DeviceDetail)envelope.bodyIn;
  //TODO: in responseObject now your data.
  String ssIPAD = responseObject.IsIPAD ;

在这里你可以找到ksoap turorial并做一些解释: http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html