Back to project page SeeKampf.
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.
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; } }