2011-10-10 20 views
1

我用KSOAP2库和Web服务将返回我的字符串数组:如何用Java,Android中的KSOAP2获取字符串数组?

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

但我怎么能转化对象字符串数组(我与envelope.getResponce()了)?谢谢。

我的代码:

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

但这个代码返回例外

<?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="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> 

这对于

+0

如果你性反应然后字符串str = response.toString(); –

+0

看看我在[这] [1]问题的答案。这应该能解决你的问题。 [1]:http://stackoverflow.com/questions/5799775/ksoap2-android-cast-class-exception-soapobject/5801053#5801053 – naturlecso

回答

0

代码,你要解析的元素那么只有你可以改变,在以数组。

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

请跟随链接,并随后向那些对如何做一个好主意,

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

,如果你还停留打电话给我。

+0

我有问题 - I连接到服务器正确地,如果由我尝试获得响应的envelope.getResponce(),然后我得到异常 – user975290

+0

10-10 09:39:51.423:错误/ ___(676):System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.MissingMethodException:未找到方法:'System.Data.DataTable MCD.Framework.BL.ClientBL.YouTubeClientBL.GeMoviesWSRnd()'。 10-10 09:39:51.423:ERROR/___(676):at YouTubeApi.ChanelApi.GetChanel() 10-10 09:39:51。423:错误/ ___(676):---内部异常堆栈跟踪的结尾--- – user975290

+0

ther是没有方法名称您指定这就是为什么它显示错误,你可以粘贴你的代码,以便我可以看它 – DAS

5

为了从KSOAP响应字符串数组,

首先解析响应于SoapObject这样,

SoapObject result = (SoapObject) envelope.bodyIn; 

,然后从响应检索的第一个元素。在这种情况下,它是你的字符串数组这样,

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

ⅰ)的getProperty将返回你的整个字符串数组(它解析到SoapObject)和

ⅱ)getPropertyAsString(int index)将索引1处从字符串返回您的字符串数组等。

0
//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; 

    } 

//这里,getEnvelope()是用于检索使用包络的方法ksoap2

+0

getEnvelope()不是内置函数。它是一个用户定义的函数,它从SOAP响应返回包络对象 –

0

子类皂对象

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()]); 
    } 
} 
相关问题