Android Open Source - ChattyOwl Gson Request






From Project

Back to project page ChattyOwl.

License

The source code is released under:

MIT License

If you think the Android project ChattyOwl 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.znupy.chattyowl.network;
//www  .  j  a v  a 2  s.  co m

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

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 java.io.UnsupportedEncodingException;
import java.util.Map;


public class GsonRequest<T> extends Request<T> {
    private final Gson mGson;
    private final Class<T> mClazz;
    private final Listener<T> mListener;
    private Map<String, String> mParams;


    public GsonRequest(int method,
                       String url,
                       Map<String, String> params,
                       Class<T> clazz,
                       Listener<T> listener,
                       ErrorListener errorListener) {
        super(method, url, errorListener);
        this.mClazz = clazz;
        this.mListener = listener;
        this.mParams = params;
        mGson = new Gson();
    }


    public GsonRequest(int method,
                       String url,
                       Map<String, String> params,
                       Class<T> clazz,
                       Listener<T> listener,
                       ErrorListener errorListener,
                       Gson gson) {
        super(method, url, errorListener);
        this.mClazz = clazz;
        this.mListener = listener;
        this.mParams = params;
        mGson = gson;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return mParams;
    };

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


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




Java Source Code List

com.znupy.chattyowl.ApplicationTest.java
com.znupy.chattyowl.activities.MainActivity.java
com.znupy.chattyowl.activities.SettingsActivity.java
com.znupy.chattyowl.models.Property.java
com.znupy.chattyowl.network.BaseResponse.java
com.znupy.chattyowl.network.CommandCenter.java
com.znupy.chattyowl.network.GsonRequest.java
com.znupy.chattyowl.network.PropertiesLoader.java
com.znupy.chattyowl.network.PropertiesResponse.java
com.znupy.chattyowl.ui.SoundIndicator.java
com.znupy.chattyowl.widgets.PropertiesWidget.java
com.znupy.chattyowl.widgets.VoiceWidget.java