Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley library for Android parse xml response?

I am using volley library and getting response in XML.I want to know how we can parse response using /volley library.Thank you.

like image 237
userAndroid Avatar asked Jul 31 '13 06:07

userAndroid


1 Answers

Looking at Volley's Source:

https://android.googlesource.com/platform/frameworks/volley/+/android-4.3_r0.9/src/com/android/volley/toolbox/

If I am not mistaken, it comes with JsonObjectRequest and StringRequest, but no XMLRequest.

So, you could use StringRequest to get the response as String, and then use any XML marshalling/serializing tool (ex: Simple) to convert it to an Object.

Check out Simple - XML Marshalling tool: http://simple.sourceforge.net/download.php

If you use StringRequest, something like following should do it:

StringRequest request = new StringRequest(Request.Method.GET, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(String response) {
          // convert the String response to XML
          // if you use Simple, something like following should do it
            Serializer serializer = new Persister();
            serializer.read(ObjectType.class, response);  
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {                                
         // handle error response
       }
    }
);
queue.add(request);

Alternatively, you could create your own XMLRequest class by extending from Request, and use XML Serialization tool (like Simple) to return the Object.

Hope it helps,

like image 164
Alif Avatar answered Oct 23 '22 04:10

Alif