Android Open Source - Chopping Gson Request Task






From Project

Back to project page Chopping.

License

The source code is released under:

Apache License

If you think the Android project Chopping 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.chopping.net;
// ww w .j  av a  2 s . co  m
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.JsonSyntaxException;

import de.greenrobot.event.EventBus;


/**
 * Network commentator based on Volley.
 *
 * @param <T>
 *
 * @author Xinyue Zhao
 */
public   class GsonRequestTask<T> extends Request<T> {

  public static final String TAG = "GsonRequestTask";
  protected static final String COOKIE_KEY = "Cookie";
  private final Response.Listener<T> mSuccessListener = new Response.Listener<T>() {
    @Override
    public void onResponse(T _response) {
      EventBus.getDefault().post(_response);
    }
  };
  private static final Response.ErrorListener sErrorListener = new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError _error) {
      EventBus.getDefault().post(_error);
      logError(_error);
    }
  };
  protected final Context mContext;
  private final Class<T> mClazz;


  public GsonRequestTask(Context _context, int _method, String _url, Class<T> _clazz) {
    super(_method, _url, sErrorListener);
    setShouldCache(true);
    setTag(TAG);
    this.mClazz = _clazz;
    mContext = _context;
  }


  private static void logError(VolleyError _error) {
    NetworkResponse response = _error.networkResponse;
    if (response != null) {
      Map<String, String> headers = response.headers;
      if (headers != null && headers.size() > 0) {
        Set<String> keys = headers.keySet();
        Log.e(TAG, new StringBuilder().append("Status ").append(response.statusCode).toString());
        for (String key : keys) {
          Log.e(TAG, key + " ==> " + headers.get(key));
        }
      }
    }
  }


  @Override
  protected void deliverResponse(T _response) {
    mSuccessListener.onResponse(_response);
  }


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


  public void execute() {
    RequestQueue queue = TaskHelper.getRequestQueue();
    queue.add(this);
  }
}




Java Source Code List

com.android.internal.telephony.ITelephony.java
com.chopping.ApplicationTest.java
com.chopping.activities.BaseActivity.java
com.chopping.activities.BrightnessRefreshActivity.java
com.chopping.activities.ErrorHandlerActivity.java
com.chopping.application.BasicPrefs.java
com.chopping.application.ErrorHandler.java
com.chopping.application.IApp.java
com.chopping.application.InstalledAppReceiver.java
com.chopping.application.LL.java
com.chopping.bus.AirplaneModeOnEvent.java
com.chopping.bus.ApplicationConfigurationDownloadedEvent.java
com.chopping.bus.ApplicationConfigurationLoadingIgnoredEvent.java
com.chopping.bus.CloseDrawerEvent.java
com.chopping.bus.ExternalAppChangedEvent.java
com.chopping.bus.LinkToExternalAppEvent.java
com.chopping.bus.ReloadEvent.java
com.chopping.data.AppListItem.java
com.chopping.data.AppList.java
com.chopping.exceptions.CanNotOpenOrFindAppPropertiesException.java
com.chopping.exceptions.InvalidAppPropertiesException.java
com.chopping.exceptions.OperationFailException.java
com.chopping.fragments.AppListFragment.java
com.chopping.fragments.BaseFragment.java
com.chopping.fragments.ErrorHandlerFragment.java
com.chopping.net.GsonRequestTask.java
com.chopping.net.TaskHelper.java
com.chopping.utils.Consts.java
com.chopping.utils.DeviceUtils.java
com.chopping.utils.IncomingCallReceiver.java
com.chopping.utils.MediaUtils.java
com.chopping.utils.NetworkUtils.java
com.chopping.utils.Utils.java
com.chopping.utils.views.OneDirectionScrollView.java