Android Open Source - Tvdb-Api-Android Zipped Xml Object List Request






From Project

Back to project page Tvdb-Api-Android.

License

The source code is released under:

Apache License

If you think the Android project Tvdb-Api-Android 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.sburba.tvdbapi.xml;
//from w  w  w  .j  a  v a 2 s.  c  om
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZippedXmlObjectListRequest<T, S extends XmlObjectListParser<T>>
        extends XmlObjectListRequest<T, S> {

    public ZippedXmlObjectListRequest(S xmlParser, String url, Listener<Collection<T>> listener,
                                      ErrorListener errorListener) {
        super(xmlParser, url, listener, errorListener);
    }

    @Override
    protected Response<Collection<T>> parseNetworkResponse(NetworkResponse response) {
        try {
            Map<String, String> xmlStrings = unpackZip(new ByteArrayInputStream(response.data));
            Collection<T> resultList = mXmlParser.parseListFromXmlStrings(xmlStrings);
            return Response.success(resultList, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (IOException e) {
            return Response.error(new ParseError(e));
        } catch (XmlException e) {
            return Response.error(new ParseError(e));
        }
    }

    private Map<String, String> unpackZip(InputStream dataStream) throws IOException {
        ZipInputStream zipStream = new ZipInputStream(dataStream);
        byte[] buffer = new byte[1024];
        ZipEntry ze;
        int count;
        Map<String, String> xmlStrings = new HashMap<String, String>();
        while ((ze = zipStream.getNextEntry()) != null) {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            while ((count = zipStream.read(buffer)) != -1) {
                byteStream.write(buffer, 0, count);
            }
            xmlStrings.put(ze.getName(), byteStream.toString(getCharset()));
        }

        return xmlStrings;
    }
}




Java Source Code List

com.sburba.tvdbapi.TvdbApi.java
com.sburba.tvdbapi.TvdbItemAdapter.java
com.sburba.tvdbapi.example.App.java
com.sburba.tvdbapi.example.EpisodeListActivity.java
com.sburba.tvdbapi.example.LruBitmapCache.java
com.sburba.tvdbapi.example.SeasonListActivity.java
com.sburba.tvdbapi.example.SeriesListActivity.java
com.sburba.tvdbapi.model.Actor.java
com.sburba.tvdbapi.model.Banner.java
com.sburba.tvdbapi.model.Episode.java
com.sburba.tvdbapi.model.Season.java
com.sburba.tvdbapi.model.Series.java
com.sburba.tvdbapi.model.TvdbItem.java
com.sburba.tvdbapi.parser.ActorListParser.java
com.sburba.tvdbapi.parser.BannerListParser.java
com.sburba.tvdbapi.parser.EpisodeParser.java
com.sburba.tvdbapi.parser.SeasonListParser.java
com.sburba.tvdbapi.parser.SeriesParser.java
com.sburba.tvdbapi.util.ThreadPreconditions.java
com.sburba.tvdbapi.xml.XmlException.java
com.sburba.tvdbapi.xml.XmlObjectListParser.java
com.sburba.tvdbapi.xml.XmlObjectListRequest.java
com.sburba.tvdbapi.xml.XmlObjectParser.java
com.sburba.tvdbapi.xml.XmlObjectRequest.java
com.sburba.tvdbapi.xml.XmlRequest.java
com.sburba.tvdbapi.xml.XmlUtil.java
com.sburba.tvdbapi.xml.ZippedXmlObjectListRequest.java