Volley library for Android parse xml response?

前端 未结 4 639
一生所求
一生所求 2020-12-06 11:08

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

4条回答
  •  爱一瞬间的悲伤
    2020-12-06 12:07

    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() 
        {
            @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,

提交回复
热议问题