就我而言,我有一个soap响应,其中存储了一个“ArrayOfArrayOfString”类型的值。
它就像一个数组 A [4] [4] 。
A [0] [0] - > SERVICEID
A [0] [1] - >服务名称
A [0] [2] - > ServiceImageURL
A [0] [3] - > ServiceDecription
A [0] [4] - > ServiceIconURL
和A [4] [4]一样。
如何在android中处理这种类型的响应?
代码类似于:
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transportSE = new HttpTransportSE(URL);
transportSE.debug = true;
Log.i("WebService", "msg:try_out");
String[] columns = null;
ArrayList<String> rows = new ArrayList<String>();
try
{
Log.i("WebService", "msg:try+in");
transportSE.call(SOAP_ACTION, envelope);
Log.i("WebService", "msg:SoapObject");
SoapObject response = (SoapObject)envelope.getResponse();
Log.i("WebService", "Response");
try
{
// WhaT SHOULD I USE HERE to convert it to 2D Array//
}
catch (Exception e)
{
e.printStackTrace();
Log.v("CATCH BLOCK", e.getMessage());
}
}
catch (Exception e)
{
e.printStackTrace();
Log.i("WebService", "msg:Exception error");
Log.i("WebSerivce", e.getMessage());
return e.getMessage();
}
请帮我解决这个问题。
答案 0 :(得分:3)
这是解析复杂数据的完美工作代码......
我只是为A [0] [1-4]做这个,根据我的肥皂反应根据你的肥皂反应修改代码。
SoapObject result = (SoapObject)enevlop.getResponse();
String str = result.getProperty(0).toString();
// add a for loop for ur code and iterate it according to ur soap response and get all the node using getProperty(i);
String str1 = lameParser(str);
textView.setText(""+str1);
现在定义lameParser()方法: -
public String lameParser(String input){
String sName=input.substring(input.indexOf("sName=")+6, input.indexOf(";", input.indexOf("sName=")));
int IGoals=Integer.valueOf(input.substring(input.indexOf("iGoals=")+7, input.indexOf(";", input.indexOf("iGoals="))));
String sCountry=input.substring(input.indexOf("sCountry=")+9, input.indexOf(";", input.indexOf("sCountry=")));
String sFlag=input.substring(input.indexOf("sFlag=")+6, input.indexOf(";", input.indexOf("sFlag=")));
return sName+"\n"+Integer.toString(IGoals)+"\n"+sCountry+"\n"+sFlag;
}
答案 1 :(得分:1)
这是解析xml数据的多个子节点的代码......
public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class theClass = output.getClass();
Field[] fields = theClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Type type=fields[i].getType();
fields[i].setAccessible(true);
//detect String
if (fields[i].getType().equals(String.class)) {
String tag = "s" + fields[i].getName() + "="; //"s" is for String in the above soap response example + field name for example Name = "sName"
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
if(strValue.length()!=0){
fields[i].set(output, strValue);
}
}
}
//detect int or Integer
if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
String tag = "i" + fields[i].getName() + "="; //"i" is for Integer or int in the above soap response example+ field name for example Goals = "iGoals"
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
if(strValue.length()!=0){
fields[i].setInt(output, Integer.valueOf(strValue));
}
}
}
//detect float or Float
if (type.equals(Float.TYPE) || type.equals(Float.class)) {
String tag = "f" + fields[i].getName() + "=";
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
if(strValue.length()!=0){
fields[i].setFloat(output, Float.valueOf(strValue));
}
}
}
}
}
如果您希望帖子给我投票,以便访问者轻松找到它......
答案 2 :(得分:1)
try
{
Log.i("WebService", "Try Block");
transportSE.call(SOAP_ACTION, envelope);
Log.i("WebService", "msg:SoapObject");
SoapObject response = (SoapObject)envelope.getResponse();
Log.i("WebService", "Response on");
int totalService = response.getPropertyCount();
int i;
String str ;
String str1;
for (i = 0; i < totalService; i++)
{
str = response.getProperty(i).toString();
Log.i("WebService", "ForLoop "+ Integer.toString(i));
str1 = lameParser(str, i);
Log.i("WebService", "ForLoop: lameParser done");
Log.i("WebService", "Value Stored:: "+ str1);
}
}
catch (Exception e)
{
e.printStackTrace();
Log.i("WebService", "msg:Exception error");
Log.i("WebSerivce", e.getMessage());
}
}
private String lameParser(String input, int I)
{
int i = I;
Log.i("WebService", "LameParse()" );
try
{
String SId = input.substring(input.indexOf("{string=")+8, input.indexOf(";", input.indexOf("{string=")));
String SName = input.substring(input.indexOf(" string=")+8, input.indexOf(";", input.indexOf(" string=")));
String SIurl = input.substring(input.indexOf("http"), input.indexOf(";", input.indexOf("http")));
String SIcon = input.substring(input.indexOf("jpg; string=")+12, input.indexOf("; }", input.indexOf("jpg; string=")));
// String[][] arr = new String[x][y]; is already initialized as local var of class.
arr[i][0] = SId;
arr[i][1] = SName;
arr[i][2] = SIurl;
arr[i][3] = SIcon;
return SId + "\n" + SName + "\n" + SIurl + "\n" + SIcon + "\n" ;
}
catch (Exception e)
{
Log.i("WebService", "catch exception" );
Log.i("WebService", e.getMessage());
return null;
}
}
答案 3 :(得分:1)
以下是我处理ArrayOfArrayOfString对象的方法。
SoapObject result = (SoapObject)envelope.bodyIn;
if (result.getPropertyCount() > 0) {
SoapObject Rows = (SoapObject)result.getProperty(0);
int nRows = Rows.getPropertyCount();
for (int nRow=0; nRow<nRows; nRow++) {
SoapObject Cols = (SoapObject)Rows.getProperty(nRow);
int nCols = Cols.getPropertyCount();
for (int nCol=0; nCol<nCols; nCol++) {
String sCol = Cols.getProperty(nCol).toString();
// Process sCol with nRow and nCol as array indexes
}
}
}