3

I am working on an Android App and using Volley library. The response for some of the requests are in XML format. I searched for Android Volley tutorial on how to deal with XML, but can't seem to find.

The only option at this point for me is:

  1. Use StringRequest to get the Server Response as String
  2. Parse String and traverse it?

Here's the source of Volley: https://android.googlesource.com/platform/frameworks/volley/+/43950676303ff68b23a8b469d6a534ccd1e08cfc/src/com/android/volley/toolbox

I don't see any class dealing with XML Objects.

Any other advice. Help???

Jack H.
  • 33
  • 1
  • 3
  • 1
    This seems to be related, good luck. http://stackoverflow.com/questions/17962904/volley-library-for-android-parse-xml-response – span Dec 07 '13 at 22:43

3 Answers3

2

As someone posted in comment, it has been answered here:

Volley library for Android parse xml response?

Volley do not directly provide an XML Object. Your approach of taking the response as String and then inflating to XMLObject is the way I did it.

I made a Class for parsing XML Responses from Server (Combining GsonRequest with Simple). Here's the Class Code Snippet: SimpleXmlRequest

At first it takes the response from Server as String. Then, it uses Simple Serialization tool (http://simple.sourceforge.net/) to inflate the response to XML Object.

Community
  • 1
  • 1
Alif
  • 301
  • 2
  • 5
0

gson-xml integrated with GsonRequest may be your answer.

christiandeange
  • 1,175
  • 12
  • 17
0

This is something i wrote few weeks back to use with volley. It uses Simple Serialization for xml parsing and is very much similar to GSON.

CODE :

public class XmlGsonRequest<T> extends Request<T> {

public static final int XML_REQUEST = 1;
public static final int GSON_REQUEST = 2;

private Gson mGson;
private Serializer mSerializer;
private final Class<T> mClazz;
private final Listener<T> mListener;
private final int mRequestType;


public XmlGsonRequest(int method, int requestType, String url, Class<T> clazz, Listener<T> listener,
        ErrorListener errorListener) {
    super(method, url, errorListener);
    mClazz = clazz;
    mListener = listener;
    mRequestType = requestType;
}

@Override
protected void deliverResponse(T response) {
    mListener.onResponse(response);
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String source = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

        if (mRequestType == XML_REQUEST) {

            mSerializer = new Persister();
            Reader reader = new StringReader(source);
            return Response.success(mSerializer.read(mClazz, reader, false),
                    HttpHeaderParser.parseCacheHeaders(response));

        } else if (mRequestType == GSON_REQUEST) {

            mGson = new Gson();
            return Response.success(mGson.fromJson(source, mClazz), HttpHeaderParser.parseCacheHeaders(response));

        } else {
            return null;
        }

    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    } catch (Exception e) {
        return Response.error(new ParseError(e));
    }
}
}
ZealDeveloper
  • 783
  • 5
  • 21