可能重复:
Parsing ksoap2 response
我看过很多关于此的论坛问题,但我找不到适用于我的解决方案。 这是我的代码:
String serviceURL = "http://www.mywebsite.com/WebService.asmx";
String serviceNamespace = "http://tempuri.org/";
String serviceMethod = "getUser";
String SoapAction = "http://tempuri.org/getUser";
SoapObject SoapObj = new SoapObject(serviceNamespace,serviceMethod);
SoapObj.newInstance ();
SoapObj.addProperty ("uname", "Evv");
// SoapObj.addProperty ("uposy", "333");
//SoapObj.addProperty ("uposx", "333");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = SoapObj;
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.encodingStyle = "utf-8";
envelope.enc = SoapSerializationEnvelope.ENC2001;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);
//Initiate Connection
try
{
ht.debug = true;
ht.call (SoapAction, envelope);
SoapObject response=(SoapObject)envelope.getResponse();
tv.setText(response);
}
是anyType {uposx = 222; uposy = 222;} JSON响应?我可以在textview中看到它,但我想解析它。
我尝试过使用JSON对象,但没有让它起作用:
JSONObject JSONObj;
JSONArray JSONArr;
SoapObject res = (SoapObject)envelope.getResponse();
gets our result in JSON String
String ResultObject = response.getProperty(0).toString();
if (ResultObject.startsWith("anyType{")) { // if JSON string is an object
JSONObj = new JSONObject(ResultObject);
Iterator<String> itr = JSONObj.keys();
while (itr.hasNext()) {
String Key = (String) itr.next();
String Value = JSONObj.getString(Key);
bundleResult.putString(Key, Value);
tv.setText(bundleResult.getString(Key));
}
} else if (ResultObject.startsWith("anyType{")) { // if JSON string is an array
JSONArr = new JSONArray(ResultObject);
tv.setText("length" + JSONArr.length());
for (int i = 0; i < JSONArr.length(); i++) {
JSONObj = (JSONObject) JSONArr.get(i);
bundleResult.putString(String.valueOf(i), JSONObj.toString());
// System.out.println(bundleResult.getString(i));
}
}
感谢您的帮助!