Using ksoap2 for android, and parsing output data


SDK Version: 
M3

So the other day, I was asked to check out how we could use soap on Android, preferably with ksoap2 for android, and a public SOAP Web Service. For the latter the TopGoalScorers web service was chosen.

This example will prepare a soap message with one extra variable and value (iTopN, 5) and get a soap object as response.

  1. private static final String METHOD_NAME = "TopGoalScorers";
  2.  private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/TopGoalScorers";
  3.  private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
  4.  private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL";
  5. //you can get these values from the wsdl file^
  6.  
  7.  public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {
  8.     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
  9.     request.addProperty("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
  10.     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
  11.     envelope.setOutputSoapObject(request);  //prepare request
  12.     AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);  
  13.     httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump)
  14.     httpTransport.call(SOAP_ACTION, envelope); //send request
  15.     SoapObject result=(SoapObject)envelope.getResponse(); //get response
  16.     return result;
  17.  }
  18.  
  19. //usage:
  20. //SoapObject result=soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
  21. //don't forget to catch the exceptions!!

So after I managed to get a proper soap response, it was time to parse it. I couldn't find any proper already made method to parse it, that actually worked.

You can get the soap objects elements, line by line like this...

  1. result.getProperty(elementNumber).toString();

...but, this is it! There is no getValue(), or anything like it that would even resemble a parser.

A response soap message from ksoap2 looks like this:

  1. anyType{tTopGoalScorer=anyType{sName=Gonzalo HiguaÃn; iGoals=3; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/ar.gif; }; tTopGoalScorer=anyType{sName=Asamoah Gyan; iGoals=2; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/gh.gif; }; tTopGoalScorer=anyType{sName=Brett Holman; iGoals=2; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/au.gif; }; tTopGoalScorer=anyType{sName=Chung-Yong Lee; iGoals=2; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/kr.gif; }; tTopGoalScorer=anyType{sName=David Villa; iGoals=2; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/es.gif; }; }

Pretty well structured, but a pain in the ass to work with.
The official documentation suggests a way to implement and register an object with the Marshal interface, but after seeing the string representation above, it seems to be way too complicated. Let's see how we could make things easier, and more importantly, let's try to achieve it in a generalized way.

This is one element:

  1. {sName=Gonzalo HiguaÃn; iGoals=3; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/images/flags/ar.gif; }

Obviously I could make something like this fast, but this is only works for the example service.

  1. //parse the soap object, one element at a time.
  2.  public String lameParser(String input){
  3.     String sName=input.substring(input.indexOf("sName=")+6, input.indexOf(";", input.indexOf("sName=")));
  4.     int IGoals=Integer.valueOf(input.substring(input.indexOf("iGoals=")+7, input.indexOf(";", input.indexOf("iGoals="))));
  5.     String sCountry=input.substring(input.indexOf("sCountry=")+9, input.indexOf(";", input.indexOf("sCountry=")));
  6.     String sFlag=input.substring(input.indexOf("sFlag=")+6, input.indexOf(";", input.indexOf("sFlag=")));
  7.     return sName+" "+Integer.toString(IGoals)+" "+sCountry+" "+sFlag;
  8.  }

..but doing this every time, for every response message type, is just multiplying code, and too much work anyway.

After wondering a couple of minutes over the code, it looked as we could create a general method with reflection to do the job.
The method above receives one line of string from the result object, and a class, that has public members. To be able to map the SOAP response values with the corresponding members, the names and types of the member fields have to be exactly the same, as they are in the result response. As in the first example, we used the TopGoalScorers method, to test the parser, it had the following fields: sName, iGoals, sCountry, sFlag. For our web service example the result class (aka Business Object) is:

  1. package com.helloandroid.soaptest;
  2.  
  3. public class TopGoalScores {
  4.     String Name;
  5.     int Goals;
  6.     String Country;
  7.     String Flag;
  8. }

And here comes the parser implementation. Please note, that in the current form this only works with primitive types, like String, int, Integer, float and Float.

  1. package com.helloandroid.ksoap2;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Type;
  5.  
  6. /**
  7.  * Ksoap2 for android - output parser
  8.  * This class parses an input soap message
  9.  * @author tamas.beres@helloandroid com, akos.birtha@helloandroid com
  10.  *
  11.  */
  12. public class Ksoap2ResultParser {
  13.  
  14.     /**
  15.     * Parses a single business object containing primitive types from the response
  16.     * @param input soap message, one element at a time
  17.     * @param theClass your class object, that contains the same member names and types for the response soap object
  18.     * @return the values parsed
  19.     * @throws NumberFormatException
  20.     * @throws IllegalArgumentException
  21.     * @throws IllegalAccessException
  22.     * @throws InstantiationException
  23.     */
  24.     public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
  25.  
  26.         Class theClass = output.getClass();
  27.         Field[] fields = theClass.getDeclaredFields();
  28.        
  29.         for (int i = 0; i < fields.length; i++) {
  30.             Type type=fields[i].getType();
  31.             fields[i].setAccessible(true);
  32.  
  33.             //detect String
  34.             if (fields[i].getType().equals(String.class)) {
  35.                 String tag = "s" + fields[i].getName() + "=";   //"s" is for String in the above soap response example + field name for example Name = "sName"
  36.                 if(input.contains(tag)){
  37.                     String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
  38.                     if(strValue.length()!=0){
  39.                         fields[i].set(output, strValue);
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             //detect int or Integer
  45.             if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
  46.                 String tag = "i" + fields[i].getName() + "=";  //"i" is for Integer or int in the above soap response example+ field name for example Goals = "iGoals"
  47.                 if(input.contains(tag)){
  48.                     String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
  49.                     if(strValue.length()!=0){
  50.                         fields[i].setInt(output, Integer.valueOf(strValue));
  51.                     }
  52.                 }
  53.             }
  54.  
  55.             //detect float or Float
  56.             if (type.equals(Float.TYPE) || type.equals(Float.class)) {
  57.                 String tag = "f" + fields[i].getName() + "=";
  58.                 if(input.contains(tag)){
  59.                     String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
  60.                     if(strValue.length()!=0){
  61.                         fields[i].setFloat(output, Float.valueOf(strValue));
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.        
  67.     }
  68. }

Now this class can be easily reused for parsing any response with primitive types, and it provides the result in a convenient form to work with.
Usage:

  1.  TopGoalScores topGoalScores=new TopGoalScores();
  2.  
  3.  try {
  4.     Ksoap2ResultParser.parseBusinessObject(soapresultmsg.getProperty(0).toString(), topGoalScores);
  5.  
  6.  } catch (NumberFormatException e) {
  7.     // TODO Auto-generated catch block
  8.     e.printStackTrace();
  9.  } catch (IllegalArgumentException e) {
  10.     // TODO Auto-generated catch block
  11.     e.printStackTrace();
  12.  } catch (IllegalAccessException e) {
  13.     // TODO Auto-generated catch block
  14.     e.printStackTrace();
  15.  } catch (InstantiationException e) {
  16.     // TODO Auto-generated catch block
  17.     e.printStackTrace();
  18.  }

We know, that there is still room for improvements. The error reporting could be done in a sophisticated way for example. Or maybe we could do an implementation, that not only parses one object, but a list of objects. With this article we just wanted to introduce the basic idea.

Comments


نصرت البدر

حسام الرسام
جي فاير
كاظم الساهر
محمد السالم
فضائح الفنانين
فضائح ممثلات عاريات
اغتصاب
فضائح المشاهير
تحميل الاغاني العراقية
اغاني عراقية
ابراج الحظ,برجك اليوم
صور الفنانات,صور الممثلين
نكت عراقية
التعارف والاصدقاء
مسلسلات
صور ممثلات
صور فنانين
ابراج اليوم
الدليل العراقي
اغاني عراقية
دليل المواقع العراقية
جات عراقي
دردشة عراقية
شات العراق
دردشة العراق
اغاني عراقية mp3
اغاني mp3
عراق اب
اغاني عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
دردشة ذي قار
دردشة العاشق
دردشة عراقية
دردشة عراق3
دردشة موسوعة الخليج
عراق الرومانسية
دردشة شط العرب
دردشة دجلةدردشة
شات عراقي
جات عراقي
موقع العاشق
دردشة العاشق
ابراج
نكت عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
دردشة كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
الدردشات العراقية
دردشة ذي قار

دردشة كويتية

دردشة كويتية
كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
دردشة كويت 777
شات كويت 777
الكويت 777
موقع الكويت 777
موقع الكويت25
دردشة كويت 25
شات كويت 25
دردشة سعودية
صبايا العراق
دردشة صبايا العراق
دردشة بنوتة سعودية
دردشة السعودية
دردشة المدينة
دردشة تبوك
دردشة جيزان
دردشة نجران
دردشة القطيف
دردشة القصيم
دردشة الجوف
دردشة الحائل
دردشة الباحة
دردشة عسير
دردشة الشرقية
دردشة مكة
دردشة الرياض
دردشة عراقنا
عراقنا
شات عراقنا
موقع عراقنا
جات عراقنا
شات العراقا

دردشة عراقية

Thanks I really enjoy share for my friends and post on my blog.
parduotuvė

This is my Datatable xml style

<?xml version="1.0" encoding="utf-8" ?>

-

and wsdl

Same like above article I did , I got this kind of error message when I call this method,

response = soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, APPURL);
Log.w("log_tag","*********" + response.getProperty(0).toString());

// ksoap2 calling wcf
public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
request.addProperty("strExec", "7067");
request.addProperty("strBusinessUnit", "HEMA");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
httpTransport.debug = true;

try{
Log.w("log_tag", " ===========" +SOAP_ACTION.toString() );
Log.w("Log_cat" ,"*********" + envelope.toString());
httpTransport.call(SOAP_ACTION, envelope);

}
catch(Exception e)
{
e.printStackTrace();
}
SoapObject responses = (SoapObject)envelope.getResponse();
return responses;

}
Error is :

anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }

please help me this.

Thanks in advance;

another great tutorial Joomla template maker.

شات صوتي

دردشة صوتية

دردشة

دردشه

شات سعودي

شات خليجي
سكر بنات

جات

شات صوتي سعودي خليجي

chat voice

ahj

خليجي الصوتي

سعودي الصوتي

دردشة صوتي

شات صوتي
دردشة صوتية
شات كتابي

شات كتابي خليجي

شات عسل الصوتي

دردشة كتابية

chat
سعودي كول
سعودي كول 6666

كول

سعودي

سعودي كول انحراف

سعودي كول بنات

سعودي كول 1994

سعودي كول 94

شات سعودي كول

سعودي انحراف

سعودي انحراف2010

سعودي انحراف الصوتي

شات سعودي انحراف

دردشة سعودي انحراف

سعودي انحراف الصوتية

شبكة سعودي انحراف

سعودي انحراف الاصلي

سعودي انحراف كول

سعودي انحراف 2010

انحراف سعودي

saudideviation

دردشة صوتية سعوديه
دردشة صوتية سعودية

دردشة كتابية
دردشة كتابية خليجية
شات
دردشة
خاص للبنات

عرب ذوق

عرب ذوق الصوتي

عرب ذوق الصوتية

دردشة عرب ذوق

شات عرب ذوق

شبكة عرب ذوق

شات صوتي بنات

شات بنات الصوتي

دردشة بنات الصوتي

Girls Chat

شبكة عفناك

صوتية عفناك

شات عفناك

دردشة عفناك

عفناك الصوتي

دردشة عفناك

الخيال
الخيال كام
شبكة الخيال
الخيال الصوتي
الخيال الصوتية
دردشة الخيال
الخيال الصوتية
دردشة صوتية الخيال

شات سعودي خليجي

منتدى نونو

منتدى

منتديات

موقع

شبكة

نونو

Chat Nono

ahj w,jd

]v]am w,jdm

دليل مواقع ويب

دليل مواقع

دليل

مواقع

بنت كول
بنت كول الصوتي
شات بنت كول

دردشة بنت كول
شات بنت كول الصوتية

بنت كول الصوتيه
سعودي كول
صوتية سعودي كول
شات سعودي كول
دردشة سعودي كول
سعودي كول الصوتي
سعودي كول 6666
سعودي كول6666
سكر بنات
شات صوتي زين
شات صوتي ملوك
شات صوتي سعودي
شاتات صوتيه
مكتبة ماسنجر
شات صوتي حبي
شات صوتي كويت
YouTube - Broadcast Yourself.‏ , اليوتيوب نونو
صيف كام
شات صوتي كول
شات انحراف
وه بس
خريطة الموقع نونو
الرياض كول الصوتي
كامات 6666
شات المها
كامات6666
شات كامات 6666
كامات 666
كامات 66
سعودي انحراف
شاتكامات6666
سعودي احوه
شات سعودي احوه
سعودي احوه الصوتي
سعودي احوه كول
دردشة سعودي احوه
احوه سعودي
بنات احوه
دبي الصوتي
سعودي في اي بي الصوتي
شبكة الرياض الصوتي
روعة الليل
لايف كام
الخليج كام
شات كان زمان الصوتي
شات صوتي قصيمي
شات قلبي
ارجوان
شات صوتي قطري
بدور الخليج

منتدى روح

شبكة روح

روح ديزاين

تحميل ماسنجر بلس

توبيكات حزينه

توبيكات

ماسنجر

ماسنجر بلس

تحميل ماسنجر

توبيكات رومنسيه

منتديات روح
دردشة
شات سعودي
خليجي
شات صوتي
سعودي انحراف 2010 , صوتية سعودي انحراف , شبكة سعودي انحراف , موقع سعودي انحراف , سعودي انحراف لايف , مواقع انحرافية , دردشة شات سعودي انحراف الصوتي , سعودي لايف 2010 , سعودي انحراف 2011 , بنات لايف , منحرفات لايف جاتسعودي انحراف 6666- سعودي انحراف 2010 - سعودي انحراف2010 - سعودي انحراف 2007 - سعودي فور انحراف - سعودي انحراف - Saudi an7raf سعودي انحراف , deviation Saudi 2010‏ دردشة, شات, سعودي, انحراف 2010, كامات, ,شات انحرافي saudi, an7raf, deviation.‏ سعودي انحراف , انحراف , An7raf 6666‏سعودي انحراف , شات سعودي انحراف , شات انحرافي , انحراف 2010سعودي انحراف , سعودي انحراف 2010 , chat saudi an7raf , saudi ...‏

Thanks for this clean tutorial. However, don't you think using reflection on a constrained device like a mobile phone is pretty expensive?
you shall check this out!gps,

Really good tutorial of creation. A code very interesting. viagra viagra pris apotek viagra trial offer offer.

it is sometimes hard to Pandora in a world of adults Pandora Bracelets

Hello Tamas,

I am not able to run your example successfully. If you can please give a final file to create. Its very urgent i am getting the same response from a .netwebservice, but not able to parse.

would look for the final file.

Thanks Gurvinder

Hi,

Congrats on your work. Thanks a lot. There is very little doc on this issue.

I have successfully run your code.

However, when i try to use the same approach i get the problem as described here:

http://stackoverflow.com/questions/3564467/soap-wev-service-on-android

Could anybody help?

Thanks

Hi,

I have been strucked for few days.But your wonderful blog supported me more.I would like to know few things like.How can i get parsed values here?In field?Just help me this alone

Thanks for this clean tutorial.
Permit me to ask this question.
How can I access Device and network information using android?

James.

Hello

Check out this tutorial:
Calling system settings from an Android app - GPS example
http://www.helloandroid.com/tutorials/calling-system-settings-android-ap...

What kind of network information do you need?

Your approach is pretty clean but you have to consider that using reflection on a constrained device like a mobile phone is pretty expensive. You could use marshalling objects as well but the fastest is probably the simple parse..

Thank you for your reply, we know that using reflection is slower. Accordind to our mesaurement, the old parser took 31ms and the reflection took around 65ms
In case of a simple businness object this may be acceptable. Everyone has to pick a side, speed, or development speed. :)

Just in case you run into trouble with attribute support I thought I mention that I have patched the library to work with attributes as well. More details are on my blog.

Ideally I would like to get the patch upstream but I have been unable to contact the google code project owner. If you have any more luck please get Karl to contact me.