如何在 Java、Android 中使用 KSOAP2 获取字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7710741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get string array with KSOAP2 in Java, Android?
提问by user975290
I use KSOAP2 library, and web-service returns me string array:
我使用 KSOAP2 库,Web 服务返回字符串数组:
<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.
但是我怎样才能将对象(我用信封.getResponce()得到)转换为字符串数组?谢谢你。
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="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
这个页面的代码
采纳答案by DAS
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,
请按照链接和后续链接了解如何操作,
if you are still stuck get back to me.
如果你仍然被卡住,请回复我。
回答by RootCode
To get the string array from ksoap response,
要从 ksoap 响应中获取字符串数组,
First parse the response to SoapObject like this,
首先像这样解析对SoapObject的响应,
SoapObject result = (SoapObject) envelope.bodyIn;
and then retrieve the first element from response. In this case it is your String array like this,
然后从响应中检索第一个元素。在这种情况下,它是像这样的 String 数组,
String str = ((SoapObject)result.getProperty(0)).getPropertyAsString(1);
i) getProperty will return your whole String array (parse it to SoapObject
) and
i) getProperty 将返回您的整个 String 数组(将其解析为SoapObject
)和
ii) getPropertyAsString(int index)
will return your String at index 1 from String array and so on.
ii)getPropertyAsString(int index)
将从字符串数组中的索引 1 处返回您的字符串,依此类推。
回答by Sreejith M Sreedharan
//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
//这里getEnvelope()
是使用ksoap2检索信封的方法
回答by piero steffano del aguila mach
subclass Soap object
子类 Soap 对象
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()]);
}
}