我用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>
這對於
頁
如果你性反應然後字符串str = response.toString(); –
看看我在[這] [1]問題的答案。這應該能解決你的問題。 [1]:http://stackoverflow.com/questions/5799775/ksoap2-android-cast-class-exception-soapobject/5801053#5801053 – naturlecso