I am using volley library and getting response in XML.I want to know how we can parse response using /volley library.Thank you.
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With