Using ksoap2 for android, and parsing output data
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.
- private static final String SOAP_ACTION = "http://footballpool.data
access.eu/data/TopGoalScorers& quot;; - //you can get these values from the wsdl file^
- SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
- request.addProperty("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
- envelope.setOutputSoapObject(request); //prepare request
- 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)
- httpTransport.call(SOAP_ACTION, envelope); //send request
- SoapObject result=(SoapObject)envelope.getResponse(); //get response
- return result;
- }
- //usage:
- //SoapObject result=soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
- //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...
- 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:
- anyType{tTopGoalScorer=anyType{sName=Gonzalo HiguaÃn; iGoals=3; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/i
mages/flags/ar.gif; }; tTopGoalScorer=anyType{sName=A samoah Gyan; iGoals=2; sCountry=Y; sFlag=http://footballpool.data access.eu/images/flags/gh.gif; }; tTopGoalScorer=anyType{sName=B rett Holman; iGoals=2; sCountry=Y; sFlag=http://footballpool.data access.eu/images/flags/au.gif; }; tTopGoalScorer=anyType{sName=C hung-Yong Lee; iGoals=2; sCountry=Y; sFlag=http://footballpool.data access.eu/images/flags/kr.gif; }; tTopGoalScorer=anyType{sName=D avid Villa; iGoals=2; sCountry=Y; sFlag=http://footballpool.data access.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:
- {sName=Gonzalo HiguaÃn; iGoals=3; sCountry=Y; sFlag=http://footballpool.dataaccess.eu/i
mages/flags/ar.gif; }
Obviously I could make something like this fast, but this is only works for the example service.
- //parse the soap object, one element at a time.
- 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=")));
- }
..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:
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.
- package com.helloandroid.ksoap2;
- import java.lang.reflect.Field;
- import java.lang.reflect.Type;
- /**
- * Ksoap2 for android - output parser
- * This class parses an input soap message
- * @author tamas.beres@helloandroid com, akos.birtha@helloandroid com
- *
- */
- public class Ksoap2ResultParser {
- /**
- * Parses a single business object containing primitive types from the response
- * @param input soap message, one element at a time
- * @param theClass your class object, that contains the same member names and types for the response soap object
- * @return the values parsed
- * @throws NumberFormatException
- * @throws IllegalArgumentException
- * @throws IllegalAccessException
- * @throws InstantiationException
- */
- public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
- Class theClass = output.getClass();
- for (int i = 0; i < fields.length; i++) {
- Type type=fields[i].getType();
- fields[i].setAccessible(true);
- //detect String
- 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
- 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){
- }
- }
- }
- //detect float or Float
- if(input.contains(tag)){
- String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
- if(strValue.length()!=0){
- }
- }
- }
- }
- }
- }
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:
- TopGoalScores topGoalScores=new TopGoalScores();
- try {
- Ksoap2ResultParser.parseBusinessObject(soapresultmsg.getProperty(0).toString(), topGoalScores);
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
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.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Maudyp17 (Malcolm Yong)I have just discovered territory 3 in My Railway! You can get it for FREE on Android! http://t.co/lWE84eeL #android #androidgames
6 min 16 sec ago -
@USandroidtablet (USA Android Tablet)USA Android: Android OS 2.3 Touch Screen 7" inch Mid Tablet PC HDMI WIFI MID 1GHz 512MB 4GB #android http://t.co/9sDVUCbP #android #tabletpc
6 min 17 sec ago -
@NeeNeeDahl (Carmen M. Dahl)I've just received an achievement: Cartographer https://t.co/CaoA5G0q #Android #Androidgames
6 min 19 sec ago -
@AshleyLynnLytle (Ashley Lytle)
just reached level 9 on Rock The Vegas on my Android http://t.co/r86NCvaT #Android #Androidgames
6 min 19 sec ago -
@belindassy (Belinda Soon)I have just connected Farm in My RailWay. You can get it for FREE on Android! http://t.co/Y7Sw9NjS #android #androidgames
6 min 20 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix




Comments
دردشة سورية دردشة
دردشة سورية
دردشة لبنانية
دردشة عراقية
شات سوري
شات لبناني
دردشة سوريا
دردشة لبنان
شات سوريا
شات لبنان
دردشة السويدي
منتديات السويدي
اغاني عراقية
صور فنانين
الرياضة العراقية
شعراء العراق
نغمات عراقية
اغاني عربية
اغاني كردية
دردشة عراقية
دردشة بنات العراق
دردشة صبايا بغداد
دردشة البصره
دردشة بغداد
دردشة بغدادية
دردشة صبايا بغداد
دردشة شباب العراق
دردشة بنات العراق
دردشة الكرادة
دردشة دمشق
دردشة بيروت
دردشة حلب
دردشة حلب
دردشة عراقية
دردشة العراق
شات عراقي
جات عراقي
دردشه عراقيه
دردشة صبايا لبنان
دردشة بنات لبنان
شات صوتي | دردشة صوتية | كلام
شات صوتي
| دردشة صوتية
|
كلام
| شات كلام
|
دردشة كلام
| دردشة صوتية
|
شات صوتي
| شات
|
Chat Voice
| ahj w,jd
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
دردشه
| دردشة
|
صوتي
| صوتية
|
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
الكلام
| دردشه صوتيه
|
]v]am w,jdm
| ]v]ai w,jdi
شات صوتي
شات صوتي
شات صوتي
نصرت البدر حسام الرسام جي
نصرت البدر
حسام الرسام
جي فاير
كاظم الساهر
محمد السالم
فضائح الفنانين
فضائح ممثلات عاريات
اغتصاب
فضائح المشاهير
تحميل الاغاني العراقية
اغاني عراقية
ابراج الحظ,برجك اليوم
صور الفنانات,صور الممثلين
نكت عراقية
التعارف والاصدقاء
مسلسلات
صور ممثلات
صور فنانين
ابراج اليوم
الدليل العراقي
اغاني عراقية
دليل المواقع العراقية
جات عراقي
دردشة عراقية
شات العراق
دردشة العراق
اغاني عراقية mp3
اغاني mp3
عراق اب
اغاني عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
دردشة ذي قار
دردشة العاشق
دردشة عراقية
دردشة عراق3
دردشة موسوعة الخليج
عراق الرومانسية
دردشة شط العرب
دردشة دجلةدردشة
شات عراقي
جات عراقي
موقع العاشق
دردشة العاشق
ابراج
نكت عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
دردشة كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
الدردشات العراقية
دردشة ذي قار
دردشة كويتية
دردشة كويتية
كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
دردشة كويت 777
شات كويت 777
الكويت 777
موقع الكويت 777
موقع الكويت25
دردشة كويت 25
شات كويت 25
دردشة سعودية
صبايا العراق
دردشة صبايا العراق
دردشة بنوتة سعودية
دردشة السعودية
دردشة المدينة
دردشة تبوك
دردشة جيزان
دردشة نجران
دردشة القطيف
دردشة القصيم
دردشة الجوف
دردشة الحائل
دردشة الباحة
دردشة عسير
دردشة الشرقية
دردشة مكة
دردشة الرياض
دردشة عراقنا
عراقنا
شات عراقنا
موقع عراقنا
جات عراقنا
شات العراقا
دردشة عراقية
Grate site.
Thanks I really enjoy share for my friends and post on my blog.
parduotuvė
Android call C# WCF it particular method return DataTable
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); ng());
Log.w("log_tag","*********" + response.getProperty(0).toStri
// 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 "7067"); ssUnit", "HEMA"); Envelope.VER11); //put all required data into a soap envelope equest);
request.addProperty("strExec",
request.addProperty("strBusine
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(Soap
envelope.dotNet = true;
envelope.setOutputSoapObject(r
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
httpTransport.debug = true;
try{ , envelope);
Log.w("log_tag", " ===========" +SOAP_ACTION.toString() );
Log.w("Log_cat" ,"*********" + envelope.toString());
httpTransport.call(SOAP_ACTION
} se();
catch(Exception e)
{
e.printStackTrace();
}
SoapObject responses = (SoapObject)envelope.getRespon
return responses;
}
Error is :
anyType{element=anyType{comple xType=anyType{choice=anyType{e lement=anyType{complexType=any Type{sequence=anyType{element= anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }
please help me this.
Thanks in advance;
tut
another great tutorial Joomla template maker.
NONO
شات صوتي
دردشة صوتية
دردشة
دردشه
شات سعودي
شات خليجي
سكر بنات
جات
شات صوتي سعودي خليجي
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 ...
Too expensive
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
Really good tutorial of creation. A code very interesting. viagra viagra pris apotek viagra trial offer offer.
it is sometimes hard to
it is sometimes hard to Pandora in a world of adults Pandora Bracelets
Request For Final Code
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
what about soap header arguments?
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/quest ions/3564467/soap-wev-service- on-android
Could anybody help?
Thanks
Small help
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
Accessing device and network information
Thanks for this clean tutorial.
Permit me to ask this question.
How can I access Device and network information using android?
James.
re
Hello
Check out this tutorial: torials/calling-system-setting s-android-ap...
Calling system settings from an Android app - GPS example
http://www.helloandroid.com/tu
What kind of network information do you need?
Reconsider reflection..
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..
re
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. :)
Missing attribute support
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.