11

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

4 回答 4

13

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,

于 2013-08-02T07:01:15.927 回答
10
    StringRequest req = new StringRequest(Request.Method.GET, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(String response) {
            processData(response);
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {                                
         // handle error response
       }
    }
);

in processdata method parse the response.Use simple sax parser or dom parser to parse the response string.

于 2014-11-10T09:29:15.233 回答
2

Here is my solution. As Sir Alif said above, volley can provide you with raw string of XML data, received from the server. So, all you need to do is to parse this String accordinly to your app logic. So, for parsing the String I used SAX parser. I'm not going to describe here how to use SAX parser cause you can find tons of tutorials by yourself, I'll just describe the key point. The piece of code below shows the moment when you create instances of SAXParserFactory, SAXParser, XMLReader etc., also here you create an instance of InputSource, and this is the key point. Commonly, you open a connection, using certain url address, and then SAX parser parses received data for you. But, as far as we are trying to use Volley library, we will give InputSource not the InputStream, but a StringReader (full information about input source and its public constructors you can find here). It will look like this:

public void makeList(String s){   
   SAXParserFactory factory = SAXParserFactory.newInstance();
   SAXParser sp = factory.newSAXParser();
   XMLReader xmlReader = sp.getXMLReader();
   SaxHandler handler = new SaxHandler();
   xmlReader.setContentHandler(handler);                               
   InputSource is = new InputSource(new StringReader(s));
   is.setEncoding("UTF-8");
   xmlReader.parse(is); 
}

So, in the Activity, where you get the StringRequest from Volley, in onRespone() method you can pass that response to the makeList(String s) method (or to whatever you called the method with the same functionality), which will parse you that response. Hope this helps! If you have some questions please ask in comments.

于 2014-11-10T03:07:53.573 回答
0

First you have to get response string by:

  StringRequest req = new StringRequest(Request.Method.GET, url, 
new Response.Listener<String>() 
{
    @Override
    public void onResponse(String response) {
         InputStream is = convertStringToDocument(reponse);
         // now you can parse xml 
    }
}, 
new Response.ErrorListener() 
{
     @Override
     public void onErrorResponse(VolleyError error) {                                
     // handle error response
   }
}

);

after getting respone via volley you have to convert String response to input stream by this method:

 private static InputStream convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try
    {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Source xmlSource = new DOMSource(doc);
        Result outputTarget = new StreamResult(outputStream);
        TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
        return is;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
于 2016-12-08T08:33:34.670 回答