我有以下肥皂输出:
tabResponse
{
tabResult=anyType
{
schema=anyType
{
element=anyType
{complexType=anyType
{
choice=anyType
{
element=anyType
{
complexType=anyType
{
sequence=anyType
{
element=anyType{};
element=anyType{};
element=anyType{};
element=anyType{};
};
};
};
};
};
};
};
diffgram=anyType
{
DocumentElement=anyType
{sh=anyType
{SchoolName=Bialik Hebrew Day School; SchoolType=Public elementary; SchoolGrade=JK-12; City=Toronto;
};
sh=anyType
{SchoolName=Millwood Junior School; SchoolType=Public elementary; SchoolGrade=JK-12; City=Toronto;
};
sh=anyType
{SchoolName=Dovercourt Junior Public School; SchoolType=Public elementary; SchoolGrade=JK-12; City=Toronto;
};
sh=anyType{
SchoolName=John English Junior Middle School; SchoolType=Public elementary; SchoolGrade=JK-12; City=Toronto;
};
sh=anyType
{SchoolName=Ossington Old Orchard Public School; SchoolType=Public elementary; SchoolGrade=JK-12; City=Toronto;
};
};
};
};
}
我的代码是这样的:
SoapDemoActivity.java
public class SoapDemoActivity extends Activity {
private final String NAMESPACE = "****";
private final String URL = "*******";
private final String SOAP_ACTION_1 = "********";
private final String SOAP_ACTION_5 = "*******";
private final String METHOD_NAME_5 = "******";
Button mButtonTab;
TextView mTextViewResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButtonTab=(Button)findViewById(R.id.button5);
mButtonTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getDataTable(NAMESPACE,METHOD_NAME_5,SOAP_ACTION_5,URL);
}
});
}
public void getDataTable(String NAMESPACE, String METHOD_NAME,
String SOAP_ACTION, String URL) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("uid","29");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.bodyIn;
if(response != null)
{
SoapObject tabResponse = (SoapObject) response.getProperty(0);
SoapObject tabResult = (SoapObject) tabResponse .getProperty(0);
SoapObject schmea = (SoapObject) tabResult .getProperty(0);
SoapObject elmemnt = (SoapObject) schmea .getProperty(0);
SoapObject complextype = (SoapObject) elmemnt .getProperty(0);
SoapObject choise = (SoapObject) complextype .getProperty(0);
SoapObject element_1 = (SoapObject) choise .getProperty(0);
SoapObject complextype_1 = (SoapObject) element_1 .getProperty(0);
SoapObject sequence = (SoapObject) complextype_1 .getProperty(0);
SoapObject element_2 = (SoapObject) sequence .getProperty(0);
SoapObject element_3 = (SoapObject) element_2 .getProperty(0);
SoapObject element_4 = (SoapObject) element_3 .getProperty(0);
SoapObject element_5 = (SoapObject) element_4 .getProperty(0);
SoapObject diffgram = (SoapObject) element_5 .getProperty(0);
SoapObject documentelement = (SoapObject) diffgram .getProperty(0);
for(int i=0;i<documentelement.getPropertyCount();i++)
{
SoapObject getAllData = (SoapObject) documentelement.getProperty(i);
if (getAllData instanceof SoapObject) {
String data= getAllData.getProperty("sh").toString();
System.out.println("DATA "+data);
}
}
}
Object re= null;
re = envelope.getResponse();
Log.i("myApp", response.toString());
System.out.println("dfdsfd "+re.toString());
mTextViewResult.setText(re.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
我收到如下错误消息:
10-02 03:39:26.892: W/System.err(1284): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
10-02 03:39:26.892: W/System.err(1284): at java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)
10-02 03:39:26.892: W/System.err(1284): at java.util.Vector.elementAt(Vector.java:328)
10-02 03:39:26.892: W/System.err(1284): at org.ksoap2.serialization.SoapObject.getProperty(SoapObject.java:139)
10-02 03:39:26.892: W/System.err(1284): at com.demosoap.SoapDemoActivity.getDataTable(SoapDemoActivity.java:241)
10-02 03:39:26.900: W/System.err(1284): at com.demosoap.SoapDemoActivity$5.onClick(SoapDemoActivity.java:134)
此消息在我的java文件
中提供 SoapObject element_2 = (SoapObject) sequence .getProperty(0);
如何正确解析?
答案 0 :(得分:9)
SoapObject root = (SoapObject) response.getProperty(0);
SoapObject s_deals = (SoapObject) root.getProperty("diffgram");
SoapObject s_deals_1 = (SoapObject) s_deals.getProperty("DocumentElement");
System.out.println("********Count : "+ s_deals_1.getPropertyCount());
for (int i = 0; i < s_deals_1.getPropertyCount(); i++)
{
Object property = s_deals_1.getProperty(i);
if (property instanceof SoapObject)
{
SoapObject category_list = (SoapObject) property;
mStringSchoolName = category_list.getProperty("SchoolName").toString();
mStringSchoolGrade = category_list.getProperty("SchoolGrade").toString();
mStringSchoolType = category_list.getProperty("SchoolType").toString();
mStringCity = category_list.getProperty("City").toString();
System.out.println("mStringSchoolName "+mStringSchoolName);
System.out.println("mStringSchoolGrade "+mStringSchoolGrade);
System.out.println("mStringSchoolType"+mStringSchoolType);
System.out.println("mStringCity "+mStringCity);
}
}
答案 1 :(得分:1)
您可以使用此代码来解析Soap Object:
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
if(resultsRequestSOAP != null)
{
SoapObject tabResponse = (SoapObject) resultsRequestSOAP.getProperty(0);
SoapObject tabResult = (SoapObject) tabResponse .getProperty(0);
像这样,您可以浏览层次结构......
最后,当你得到元素节点时:
for(int i=0;i<elementData.getPropertyCount();i++)
{
SoapObject getAllData = (SoapObject) elementData.getProperty(i);
if (getAllData instanceof SoapObject) {
String data= getAllData.getProperty({property_name}).toString();
}
}
}
else
{
Log.i("No Response","error");
return null;
}
答案 2 :(得分:0)
您可以使用以下代码找到最佳解析肥皂的代码
public static String NAMESPACE = "http://tempuri.org/";
public static String URL ="http://www.yoururl_here_Service.svc?wsdl";
public static String SOAP_ACTION = NAMESPACE+"Service/";
Object mFillObject = mObject.getClass().newInstance();
SoapObject request = new SoapObject(Tags.NAMESPACE, MethodName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
for (Object key : mHashMap.keySet()) {
System.out.println("Params: Key : " + key.toString() + " Value : "
+ mHashMap.get(key));
request.addProperty(key.toString(), mHashMap.get(key) );
}
PropertyInfo mPropertyInfo = new PropertyInfo();
mPropertyInfo.setName("extra_key");
mPropertyInfo.setValue("extra_value");
mPropertyInfo.setType(String.class);
mPropertyInfo.setNamespace(Tags.NAMESPACE);
request.addProperty(mPropertyInfo);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE mHttpTransportSE = new HttpTransportSE(Tags.URL);
mHttpTransportSE.debug = true;
mHttpTransportSE.call(Tags.SOAP_ACTION+MethodName, envelope);
data = ((SoapObject)envelope.bodyIn).getProperty(0).toString();
mFillObject = mGson.fromJson(data, mFillObject.getClass());