Android Open Source - SeeKampf Auth Gson Request






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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 net.avedo.seekampf.utils;
//from  w  w w .j  a  v a 2s  .  c  o  m
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.avedo.seekampf.R;
import net.avedo.seekampf.core.VolleyActivity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Base64;

import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
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.JsonSyntaxException;

public class AuthGsonRequest<ServiceObject> extends Request<ServiceObject> {
  protected static final int defaultClientCacheExpiry = 1000 * 60 * 3; // milliseconds; = 3 minutes

    private Class<ServiceObject> serviceObjClass;
    private final Listener<ServiceObject> listener;
    private Gson gson;

    public AuthGsonRequest(int method, String url, Class<ServiceObject> cls, String requestBody, Listener<ServiceObject> listener,
        ErrorListener errorListener) {
      // Call parent constructor ...
        super(method, url, errorListener);
        
        // ... and save the given parameters.
        this.serviceObjClass = cls;
        this.listener = listener;
        this.gson = new Gson();        
    }

    @Override
    protected Response<ServiceObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            ServiceObject parsedGSON = gson.fromJson(jsonString, serviceObjClass);
            
            return Response.success(parsedGSON, enforceClientCaching(HttpHeaderParser.parseCacheHeaders(response), response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException je) {
            return Response.error(new ParseError(je));
        }
    }

  @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
    // Fetch the application preferences handle ...
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(VolleyActivity.getInstance());
    
    // ... and the application resources.
    Resources res = VolleyActivity.getInstance().getResources();
    
    // Fetch username and password from the preferences, ...
    String username = settings.getString(res.getString(R.string.prefs_username_key), "");
    String password = settings.getString(res.getString(R.string.prefs_password_key), "");
    
    // Create the headers map, ...
      Map<String, String> headers = new HashMap<String,String>();
      
      // ... build the Authorization header value ...
      String auth = "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
      
      // ... and add the Authorization header to the header map.
      headers.put("Authorization", auth);
      
      return headers;
  }
 
    @Override
    protected void deliverResponse(ServiceObject response) {
        listener.onResponse(response);
    }
    
  protected Cache.Entry enforceClientCaching(Cache.Entry entry, NetworkResponse response) {
    if (getClientCacheExpiry() == null) {
      return entry;
    }

    long now = System.currentTimeMillis();

    if (entry == null) {
      entry = new Cache.Entry();
      entry.data = response.data;
      entry.etag = response.headers.get("ETag");
      entry.softTtl = now + getClientCacheExpiry();
      entry.ttl = entry.softTtl;
      entry.serverDate = now;
      entry.responseHeaders = response.headers;
    } else if (entry.isExpired()) {
      entry.softTtl = now + getClientCacheExpiry();
      entry.ttl = entry.softTtl;
    }

    return entry;
  }

  protected Integer getClientCacheExpiry() {
    return defaultClientCacheExpiry;
  }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java