开发者

How to get string array with KSOAP2 in Java, Android?

I use KSOAP2 library, and web-service returns me string array:

  <GetChanelResult>
    <string>
        string
    </string>
    <string>
        string
     </string>
  </GetChanelResult>

But how can I transform Object (which I got with envelope.getResponce()) to string array? Thank you.

I have the code:

package com.nda.ut;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

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

    public static String SOAP_ACTION="http://tempuri.org/GetChanel";
    public static String METHOD_NAME="GetChanel";
    public static String NAMESPACE="http://tempuri.org/";
    public static String URL="http://www.mcds.co.il/YouTube/ChanelApi.asmx";

    TextView view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        view=(TextView)findViewById(R.id.view);

        SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);


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

        AndroidHttpTransport aht=new AndroidHttpTransport(URL);

        try {
            aht.call(SOAP_ACTION, envelope);
            SoapPrimitive result=(SoapPrimitive)envelope.getResponse();
            //if (result!=null)
            view.setText("123");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("IO", "1");
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            Log.e("XmlPullParser", "2");
        }
    }
}

But this code returned exception

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="GetChanel">
        <s:complexType />
      </s:element>
      <s:element name="GetChanelResponse">
        <s:complexType>
          <s:sequence>

            <s:element minOccurs="0" maxOccurs="1" name="GetChanelResult" type="tns:ArrayOfString" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfString">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="开发者_StackOverflow社区s:string" />
        </s:sequence>
      </s:complexType>

    </s:schema>
  </wsdl:types>
  <wsdl:message name="GetChanelSoapIn">
    <wsdl:part name="parameters" element="tns:GetChanel" />
  </wsdl:message>
  <wsdl:message name="GetChanelSoapOut">
    <wsdl:part name="parameters" element="tns:GetChanelResponse" />
  </wsdl:message>
  <wsdl:portType name="ChanelApiSoap">

    <wsdl:operation name="GetChanel">
      <wsdl:input message="tns:GetChanelSoapIn" />
      <wsdl:output message="tns:GetChanelSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ChanelApiSoap" type="tns:ChanelApiSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetChanel">
      <soap:operation soapAction="http://tempuri.org/GetChanel" style="document" />

      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ChanelApiSoap12" type="tns:ChanelApiSoap">

    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetChanel">
      <soap12:operation soapAction="http://tempuri.org/GetChanel" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>

    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ChanelApi">
    <wsdl:port name="ChanelApiSoap" binding="tns:ChanelApiSoap">
      <soap:address location="http://www.mcds.co.il/YouTube/ChanelApi.asmx" />
    </wsdl:port>
    <wsdl:port name="ChanelApiSoap12" binding="tns:ChanelApiSoap12">
      <soap12:address location="http://www.mcds.co.il/YouTube/ChanelApi.asmx" />
    </wsdl:port>

  </wsdl:service>
</wsdl:definitions>

This code for page


To get the string array from ksoap response,

First parse the response to SoapObject like this,

SoapObject result = (SoapObject) envelope.bodyIn;

and then retrieve the first element from response. In this case it is your String array like this,

String str = ((SoapObject)result.getProperty(0)).getPropertyAsString(1);

i) getProperty will return your whole String array (parse it to SoapObject) and

ii) getPropertyAsString(int index) will return your String at index 1 from String array and so on.


you have to parse the elements then only you can transform that in to an array.

 Xml.parse(response.toString(), parser);

Please follow the link and the subsequent ones to have a good idea of how to do it,

http://android.vexedlogic.com/2011/04/17/android-lists-iv-accessing-and-consuming-a-soap-web-service-i/

if you are still stuck get back to me.


//For  retrieving Single dimensional array from SOAP Response Envelope, use :

 public String[] getStringArray() throws Exception
         {
            SoapObject Table = (SoapObject)getEnvelope().bodyIn;

            String []output=null;
            if(Table!=null)
            {
                int count= Table.getPropertyCount();
                output = new String[count];
                for(int i=0;i<count;i++)
                {
                    output[i]=Table.getProperty(i).toString();
                }
            }
         return output;

}  

  // or if you want 2 dimensional array , 

   public String[][] getStringTable() throws Exception
         {
            SoapObject Table =(SoapObject)getEnvelope().bodyIn;

            String [][]output=null;
            if(Table!=null)
            {
               SoapObject row = (SoapObject) Table.getProperty(0);

               if(row!=null)
               {
                   int rCount = Table.getPropertyCount();
                   int cCount = ((SoapObject)Table.getProperty(0)).getPropertyCount();
                   output = new String[rCount][cCount];
                      for(int i=0;i<rCount;i++)
                      {
                        for(int j=0;j<cCount;j++)
                            output[i][j] =((SoapObject)    Table.getProperty(i)).getProperty(j).toString();
               }

            }
             }
         return output;

    }

//Here, getEnvelope() is the method for retrieving envelope using ksoap2


subclass Soap object

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

public class CustomSoapObject extends SoapObject {

    public CustomSoapObject(String namespace, String name) {
        super(namespace, name);
    }

    public CustomSoapObject(SoapObject soap) {
        super(soap.getNamespace(), soap.getName());
        for (int i = 0; i < soap.getPropertyCount(); i++) {
            try {
                PropertyInfo propertyInfo = new PropertyInfo();
                soap.getPropertyInfo(i, propertyInfo);
                this.addProperty(propertyInfo);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    }

    public PropertyInfo[] getPropertyAsArray(String propertyName) {
        List<PropertyInfo> res = new ArrayList<PropertyInfo>();
        for (Object property : properties) {
            if (property instanceof PropertyInfo) {
                if (((PropertyInfo) property).getName().equals(propertyName)) {
                    res.add(((PropertyInfo) property));
                }
            } else {
                System.out.println(property.toString());
            }
        }

        return res.toArray(new PropertyInfo[res.size()]);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜