Parsing ksoap2 response
Here is a tutorial on how to work with array of complex objects with KSOAP. I found out by countless hours of debugging. Hope this hepls
KSOAP Complex response parsing
I actually found out myself here are the files:
Request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("IPAddress","127.0.0.1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet= true;
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResponse", GetGeoIPResponse.class);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResult", GetGeoIPResult.class);
soapEnvelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
transport.call(SOAP_ACTION, soapEnvelope);
GetGeoIPResponse resultString = (GetGeoIPResponse) soapEnvelope.bodyIn;
GetGeoIPResult.java
public class GetGeoIPResult implements KvmSerializable {
private Integer returnCode;
private String IP;
private String returnCodeDetails;
private String countryName;
private String countryCode;
@Override
public Object getProperty(int index) {
switch (index){
case 0: return returnCode;
case 1: return IP;
case 2: return returnCodeDetails;
case 3: return countryName;
case 4: return countryCode;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 5;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
returnCode = Integer.parseInt(value.toString());
break;
case 1:
IP = value.toString();
break;
case 2:
returnCodeDetails = value.toString();
break;
case 3:
countryName = value.toString();
break;
case 4:
countryCode = value.toString();
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "ReturnCode";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "IP";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "ReturnCodeDetails";
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.name = "CountryName";
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.name = "CountryCode";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
GetGeoIPResponse.java
public class GetGeoIPResponse implements KvmSerializable {
private GetGeoIPResult getGeoIPResult;
@Override
public Object getProperty(int index) {
switch (index){
case 0:
return getGeoIPResult;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
getGeoIPResult = (GetGeoIPResult) value;
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "GetGeoIPResult";
info.type = GetGeoIPResult.class;
break;
}
}
}
KSOAP2 with array response parsing
I have same kind of issue i solve it using Vector
:
Parsing code:
ht.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
for(int i=0;i<response.getPropertyCount();i++){
Vector resV = (Vector)response.getProperty(i);
int resVlenght = resV.size();
for(int count = 0;count<resVlenght;count++)
{
HashMap<String,String> value = new HashMap<String,String>();
String n = resV.elementAt(count).toString();
SoapObject p = (SoapObject)resV.elementAt(count);
System.out.print(n);
String groom= p.getProperty("groom").toString();
}
}
How to parse Complex response with use of ksoap2 library in android
Finally i got the solution with use of KvmSerializable class,now i am able to work on nested object also with Ksoap2 here i am sharing my code which is working fine
UserDetail Serializable class for adding user tag:
public class UserDetail implements KvmSerializable {
public String Area = null;
public Long AvailableBalance = null;
public Long CityId = null;
public String Email = null;
public String FullName = null;
public String Gender = null;
public String Mobile = null;
public String Password = null;
public String Phone = null;
public String SecurityAnswer = null;
public String SecurityQuestion = null;
public String Username;
private final String DATA_NAMESPACE = "http://schemas.datacontract.org/2004/07/Jiring.MTicketing.Models";
public UserDetail() {
}
@Override
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return this.Area;
case 1:
return this.AvailableBalance;
case 2:
return this.CityId;
case 3:
return this.Email;
case 4:
return this.FullName;
case 5:
return this.Gender;
case 6:
return this.Mobile;
case 7:
return this.Password;
case 8:
return this.Phone;
case 9:
return this.SecurityAnswer;
case 10:
return this.SecurityQuestion;
case 11:
return this.Username;
}
return null;
}
@Override
public int getPropertyCount() {
return 12;
}
@SuppressWarnings("rawtypes")
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
arg2.namespace = DATA_NAMESPACE;
switch (arg0) {
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Area";
break;
case 1:
arg2.type = PropertyInfo.LONG_CLASS;
arg2.name = "AvailableBalance";
break;
case 2:
arg2.type = PropertyInfo.LONG_CLASS;
arg2.name = "CityId";
break;
case 3:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Email";
break;
case 4:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "FullName";
break;
case 5:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Gender";
break;
case 6:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Mobile";
break;
case 7:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Password";
break;
case 8:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Phone";
break;
case 9:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "SecurityAnswer";
break;
case 10:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "SecurityQuestion";
break;
case 11:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Username";
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch (arg0) {
case 0:
this.Area = arg1.toString();
break;
case 1:
this.AvailableBalance = Long.parseLong(arg1.toString());
break;
case 2:
this.CityId = Long.parseLong(arg1.toString());
break;
case 3:
this.Email = arg1.toString();
break;
case 4:
this.FullName = arg1.toString();
break;
case 5:
this.Gender = arg1.toString();
break;
case 6:
this.Mobile = arg1.toString();
break;
case 7:
this.Password = arg1.toString();
break;
case 8:
this.Phone = arg1.toString();
break;
case 9:
this.SecurityAnswer = arg1.toString();
break;
case 10:
this.SecurityQuestion = arg1.toString();
break;
case 11:
this.Username = arg1.toString();
break;
default:
break;
}
}
}
Now My Ksoap2 helper class to set http calls:
public class KSOAPHelper {
private static final String TAG = "KSOAPHelper : KSOAP Helper";
private static final String SOAP_ACTION = "http://tempuri.org/IUserService/";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "my url"
/*
* This method is use to getRespnce from request which return objects.
*/
public static Object getResponce(SoapObject request, String METHOD, final Context context) {
if (Utility.isConnectionAvailable) {
final String soap_action = SOAP_ACTION + METHOD;
Log.d(TAG, "SOAP Request : " + request);
/*
* Set the web service envelope
*/
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setAddAdornments(false);
soapEnvelope.implicitTypes = true;
soapEnvelope.setOutputSoapObject(request);
/*
* Calling of the web service and retrieve result.
*/
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
// Make the soap call.
httpTransportSE.call(soap_action, soapEnvelope);
// Get the SoapResult from the envelope body.
if (soapEnvelope.bodyIn instanceof SoapFault) {
String strFault = ((SoapFault) soapEnvelope.bodyIn).faultstring;
Log.d(TAG, "SOAP Request : " + httpTransportSE.requestDump);
Log.v(TAG, "Fault string : " + strFault);
} else {
Object object = soapEnvelope.getResponse();
return object;
}
} catch (Exception e) {
Log.d(TAG, "SOAP Request : " + httpTransportSE.requestDump);
if (e instanceof SocketException || e instanceof IOException) {
if (context instanceof Activity) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
e.printStackTrace();
}
} else {
if (context instanceof Activity) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
Log.d(TAG, "Internet Connection is not available");
}
return null;
}
This method can add nested object and will create request as i wanted.hope this will helpfull for some one who has same issue like me.thanks.
Parsing ksoap2 response into array of int
Here is a tutorial on how to work with array of complex objects with KSOAP. It will help you
Android-kSoap2 Response Parsing Issue
I was finally able to parse the Android-kSoap2
response using this
Object obj = result.getProperty(0);
Vector<SoapObject> vec = (Vector<SoapObject>) obj;
SoapObject so = (SoapObject)vec.get(0);
String memberId = so.getProperty(0).toString();
This gave me the exact Id i needed.. :)
Parse KSoap2 response in android
Take a look here, I believe this code is optimized for working with complex types and returning arrays of complex types with KSOAP:
Web Service That Returns An Array of Objects With KSOAP
Related Topics
Using Httpclient and Httppost in Android with Post Parameters
PDF Library to Rendering the PDF Files in Android
Add Object to Arraylist at Specified Index
How Many Ways to Convert Bitmap to String and Vice-Versa
How to Fix Install_Parse_Failed_Manifest_Malformed in My Android Application
Trivial: Get Confirmation of Email Sent in Android
Java Client and a C++ Server Send and Receive via Tcp Socket
Detecting Device Type in a Web Application
How to Start Two Threads at "Exactly" the Same Time
Java: Literal Percent Sign in Printf Statement
How to Use Tablayout with Viewpager2 in Android
Google Cloud Messaging - Messages Either Received Instantly or with Long Delay
Upgrade SQLite Database from One Version to Another
Android Httpclient Persistent Cookies
Binary Xml File Line #0: Error Inflating Class Imageview
Android Debugging Inetaddress.Isreachable