Last active
September 17, 2019 14:41
-
-
Save iamvivekkaushik/b0608ff18902696051856c41f3e7e332 to your computer and use it in GitHub Desktop.
Helper classes to be used with (DRF-Android)[https://github.com/101Loop/DRF-Android]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.drfUser; | |
import android.content.Context; | |
import android.util.Log; | |
import android.widget.Toast; | |
import com.android.volley.VolleyError; | |
import com.civilmachines.drfapi.DjangoErrorListener; | |
import org.json.JSONObject; | |
public class APIErrorListener extends DjangoErrorListener { | |
private static final String TAG = "APIErrorListener"; | |
private Context context; | |
public APIErrorListener(Context context) { | |
this.context = context; | |
} | |
@Override | |
public void onNetworkError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onMethodNotAllowedError(String message) { | |
defaultErrorListener(message); | |
} | |
@Override | |
public void onAuthFailureError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onBadRequestError(String message) { | |
defaultErrorListener(message); | |
} | |
@Override | |
public void onBadRequestError(JSONObject response) { | |
defaultErrorListener("Bad Request. Data sent in incorrect format!"); | |
} | |
@Override | |
public void onDefaultError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onDefaultHTMLError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onDefaultJsonError(JSONObject response) { | |
defaultErrorListener("Some JSON Error occurred."); | |
} | |
@Override | |
public void onForbiddenError(String message) { | |
defaultErrorListener(message); | |
} | |
@Override | |
public void onNoConnectionError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onNonJsonError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onNotFoundError(String message) { | |
defaultErrorListener(message); | |
} | |
@Override | |
public void onParseError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onServerError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onTimeoutError(String response) { | |
defaultErrorListener(response); | |
} | |
@Override | |
public void onUnprocessableEntityError(String message) { | |
defaultErrorListener(message); | |
} | |
@Override | |
public void onUnprocessableEntityError(JSONObject response) { | |
defaultErrorListener("Unprocessable Entity"); | |
} | |
@Override | |
public void onUnsupportedMediaTypeError(String message) { | |
defaultErrorListener(message); | |
} | |
public void defaultErrorListener(String message) { | |
Log.e(TAG, "defaultErrorListener: " + message); | |
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
try { | |
super.onErrorResponse(error); | |
} catch (Exception e) { | |
Toast.makeText(context, "Can't connect to the server.", Toast.LENGTH_SHORT).show(); | |
Log.e(TAG, "onErrorResponse: " + error.getMessage()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.drfUser; | |
import android.app.Application; | |
import android.content.Context; | |
import android.text.TextUtils; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.Volley; | |
public class VolleySingleton extends Application { | |
public static final String TAG = VolleySingleton.class | |
.getSimpleName(); | |
private RequestQueue mRequestQueue; | |
private static VolleySingleton mInstance; | |
private static Context mCtx; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mInstance = this; | |
} | |
public VolleySingleton() { | |
// required constructor | |
} | |
private VolleySingleton(Context context) { | |
mCtx = context; | |
mRequestQueue = getRequestQueue(); | |
} | |
public static synchronized VolleySingleton getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new VolleySingleton(context); | |
} | |
return mInstance; | |
} | |
public RequestQueue getRequestQueue() { | |
if (mRequestQueue == null) { | |
mRequestQueue = Volley.newRequestQueue(getApplicationContext()); | |
} | |
return mRequestQueue; | |
} | |
public <T> void addToRequestQueue(Request<T> req, String tag) { | |
// set the default tag if tag is empty | |
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); | |
getRequestQueue().add(req); | |
} | |
public <T> void addToRequestQueue(Request<T> req) { | |
req.setTag(TAG); | |
getRequestQueue().add(req); | |
} | |
public void cancelPendingRequests(Object tag) { | |
if (mRequestQueue != null) { | |
mRequestQueue.cancelAll(tag); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment