Android Open Source - RssReader Simple Xml Request






From Project

Back to project page RssReader.

License

The source code is released under:

MIT License

If you think the Android project RssReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vicmns.rssreader.http;
import java.io.UnsupportedEncodingException;
import java.util.Map;
 /*w  w w  .j  a  v a2s . c o m*/
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()));
    } 
    }
}




Java Source Code List

com.vicmns.rssreader.activities.MainActivity.java
com.vicmns.rssreader.activities.RssItemDetailsActivity.java
com.vicmns.rssreader.adapters.RssItemsAdapter.java
com.vicmns.rssreader.app.RssReaderApplication.java
com.vicmns.rssreader.http.GetRssItems.java
com.vicmns.rssreader.http.GetWidgetRssItems.java
com.vicmns.rssreader.http.SimpleXmlRequest.java
com.vicmns.rssreader.interfaces.GetRssItemsCallbacks.java
com.vicmns.rssreader.interfaces.HttpConsumerCallbacks.java
com.vicmns.rssreader.interfaces.ListOverlayLayoutCallbacks.java
com.vicmns.rssreader.models.RssItem.java
com.vicmns.rssreader.models.RssItems.java
com.vicmns.rssreader.services.GetRssItemsService.java
com.vicmns.rssreader.views.ListOverlayLayoutView.java
com.vicmns.rssreader.views.SquareImageView.java
com.vicmns.rssreader.widget.WidgetListProvider.java
com.vicmns.rssreader.widget.WidgetProvider.java
com.vicmns.rssreader.widget.WidgetService.java
fr.castorflex.android.smoothprogressbar.ColorsShape.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBarUtils.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
org.robolectric.shadows.ShadowSupportMenuInflater.java