Android Open Source - AndroidAquarium Gson Request






From Project

Back to project page AndroidAquarium.

License

The source code is released under:

MIT License

If you think the Android project AndroidAquarium 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.kypeli.aquarium.volley;
//w w w. jav a2  s .  c om
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.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.kypeli.aquarium.models.AquariumReadings;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class GsonRequest<T> extends Request<T> {
    private final Gson gson;
    private final Class<T> clazz;
    private final Listener<T> listener;

    public GsonRequest(String url,
                       Class<T> clazz,
                       Listener<T> listener,
                       ErrorListener errorListener) {
        super(Method.GET, url, errorListener);

        this.clazz = clazz;
        this.listener = listener;
        this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new AquariumReadings.TemperatureDateSerializer()).create();
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
        try {
            String json = new String(
                    networkResponse.data,
                    HttpHeaderParser.parseCharset(networkResponse.headers)
            );
            return Response.success(gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }

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




Java Source Code List

com.kypeli.aquarium.AquariumValueReader.java
com.kypeli.aquarium.MainActivity.java
com.kypeli.aquarium.TemperatureFragment.java
com.kypeli.aquarium.models.AquariumReadingsJackson.java
com.kypeli.aquarium.models.AquariumReadings.java
com.kypeli.aquarium.volley.GsonRequest.java
com.kypeli.aquarium.volley.JacksonRequest.java