-
-
Save itsalif/6149365 to your computer and use it in GitHub Desktop.
| import java.io.UnsupportedEncodingException; | |
| import java.util.Map; | |
| import org.simpleframework.xml.Serializer; | |
| import org.simpleframework.xml.core.Persister; | |
| import com.android.volley.AuthFailureError; | |
| import com.android.volley.NetworkResponse; | |
| import com.android.volley.ParseError; | |
| import com.android.volley.Request; | |
| import com.android.volley.Response; | |
| import com.android.volley.Response.ErrorListener; | |
| import com.android.volley.Response.Listener; | |
| import com.android.volley.VolleyError; | |
| import com.android.volley.toolbox.HttpHeaderParser; | |
| /** | |
| * Simple Volley Class for doing XML HTTP Requests which are parsed | |
| * into Java objects by Simple @see {{@link http://simple.sourceforge.net/} | |
| */ | |
| public class SimpleXmlRequest<T> extends Request<T> { | |
| private static final Serializer serializer = new Persister(); | |
| private final Class<T> clazz; | |
| private final Map<String, String> headers; | |
| private final Listener<T> listener; | |
| /** | |
| * Make HTTP request and return a parsed object from Response | |
| * Invokes the other constructor. | |
| * | |
| * @see SimpleXmlRequest#SimpleXmlRequest(int, String, Class, Map, Listener, ErrorListener) | |
| */ | |
| public SimpleXmlRequest(int method, String url, Class<T> clazz, | |
| Listener<T> listener, ErrorListener errorListener) { | |
| this(method, url, clazz, null, listener, errorListener); | |
| } | |
| /** | |
| * Make HTTP request and return a parsed object from XML Response | |
| * | |
| * @param url URL of the request to make | |
| * @param clazz Relevant class object | |
| * @param headers Map of request headers | |
| * @param listener | |
| * @param errorListener | |
| * | |
| */ | |
| public SimpleXmlRequest(int method, String url, Class<T> clazz, Map<String, String> headers, | |
| Listener<T> listener, ErrorListener errorListener) { | |
| super(method, url, errorListener); | |
| this.clazz = clazz; | |
| this.headers = headers; | |
| this.listener = listener; | |
| } | |
| @Override | |
| public Map<String, String> getHeaders() throws AuthFailureError { | |
| return headers != null ? headers : super.getHeaders(); | |
| } | |
| @Override | |
| protected void deliverResponse(T response) { | |
| listener.onResponse(response); | |
| } | |
| @Override | |
| protected Response<T> parseNetworkResponse(NetworkResponse response) | |
| { | |
| try { | |
| String data = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); | |
| return Response.success(serializer.read(clazz, data), | |
| HttpHeaderParser.parseCacheHeaders(response)); | |
| } | |
| catch (UnsupportedEncodingException e) { | |
| return Response.error(new ParseError(e)); | |
| } | |
| catch (Exception e) { | |
| return Response.error(new VolleyError(e.getMessage())); | |
| } | |
| } | |
| } |
Hello guys, I want to handle this xml file
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title>...</title>
<atom:link href="..." rel="self" type="application/rss+xml"/>
<link>...</link>
<description>...</description>
<lastBuildDate>Mon, 23 Mar 2015 14:17:24 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>...</generator>
<item>
<title>...</title>
<link>...</link>
<comments>...</comments>
<pubDate>...</pubDate>
<dc:creator><![CDATA[ ... ]]></dc:creator>
<category><![CDATA[ ... ]]></category>
<category><![CDATA[ ... ]]></category>
<category><![CDATA[ ... ]]></category>
<category><![CDATA[ ... ]]></category>
<category><![CDATA[ ... ]]></category>
<guid isPermaLink="false">...</guid>
<description><![CDATA[<img width="740" height="493" src="HTTP://...jpg" class="attachment- wp-post-image" alt="DSC" style="float:left; margin:0 15px 15px 0;" /> ... ]]>
</description>
<content:encoded>
<![CDATA[<img width="740" height="493" src="http://...jpg" class="attachment- wp-post-image" alt="DSC" style="float:left; margin:0 15px 15px 0;" /> bla bla...
]]>
</content:encoded>
<wfw:commentRss>...</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
....
</item>
</rss>
I want to get the<title> from the <item>and the src="..." from the <description> please tell me how to do that with your class. And how it has to be the POJO class?
I tried what allanfallas said but no success.
Can someone help me?
Hi,
I when I am running my app I am getting this
I/dalvikvm﹕ Could not find method javax.xml.stream.XMLInputFactory.newInstance, referenced from method org.simpleframework.xml.stream.StreamProvider.<init>
W/dalvikvm﹕ VFY: unable to resolve static method 14456: Ljavax/xml/stream/XMLInputFactory;.newInstance ()Ljavax/xml/stream/XMLInputFactory;
W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
I/dalvikvm﹕ Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
VFY: unable to resolve virtual method 14453: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/InputStream;)Ljavax/xml/stream/XMLEventReader;
Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
VFY: unable to resolve virtual method 14454: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/Reader;)Ljavax/xml/stream/XMLEventReader;
Is this because I don't use stax-1.2.0.jar , stax-api-1.0.1.jar , xpp3-1.1.3_8.jar ?
For the ones having the same problem as @ArcturusArc (just above me), you don't really need to worry about those messages.. if you want to know why, here you have a good reading https://robertmassaioli.wordpress.com/2011/04/21/simple-xml-in-android-1-5-and-up/ .
The first time I used this SimpleXMLRequest class I was getting those messages too, and I though that they were the reason why I wasn't getting the serialized object I wanted from the request. Anyway, I was wrong, I just forgot to define some attributes in my POJO's, so please make sure your POJO's are well defined too.
A quick note to itsalif.. This class works very well, thanks for sharing!
Hi,
how can I send xml data using volley?
Hello ,
I have following response
<APIGetCitiesResponse xmlns="http://tempuri.org/">
<APIGetCitiesResult xmlns:a="http://schemas.datacontract.org/2004/07/CRS2011AgentApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:CityPair>
<a:FromCityID>1</a:FromCityID>
<a:FromCityName>Ahmedabad</a:FromCityName>
<a:ToCityID>10</a:ToCityID>
<a:ToCityName>Amreli</a:ToCityName>
</a:CityPair>
<a:CityPair>
<a:FromCityID>1</a:FromCityID>
<a:FromCityName>Ahmedabad</a:FromCityName>
<a:ToCityID>57</a:ToCityID>
<a:ToCityName>Ankleshwar</a:ToCityName>
</a:CityPair>
</APIGetCitiesResult>
</APIGetCitiesResponse>
How to handle it ??
Can anybody help me out??.. its urgent : (
how to send POST request with XML parameter? TIA

A POJO class who works with this XML:
http://www.w3schools.com/xml/note.xml
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@root(name="note",strict = false)
public class Usuario {
}
If you need to work with a list, then read this:
http://stackoverflow.com/questions/18317273/deserializing-with-simplexml