Skip to content

Instantly share code, notes, and snippets.

@culjo
Created January 4, 2018 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save culjo/4d76d7472bfe9351c7177630dd4a1a2b to your computer and use it in GitHub Desktop.
Save culjo/4d76d7472bfe9351c7177630dd4a1a2b to your computer and use it in GitHub Desktop.
Fast Android Networking Network Call Handler
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interceptors.HttpLoggingInterceptor;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.orhanobut.logger.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import java.util.Map;
import sosan.com.chefconnect.models.UserModel;
import sosan.com.chefconnect.utility.Shortcuts;
/**
* Created by appy on 31/10/2017.
*/
public class NetworkCall {
public static final String TAG = NetworkCall.class.getSimpleName();
public static final String SUCCESSFUL_CODE = "00";
public static final String SUCCESSFUL = "SUCCESSFUL";
public static final String FAILED = "FAILED";
public static void makeGetRequest(String url, Map<String, String> params, JSONArrayRequestListener jsonArrayRequestListener) {
AndroidNetworking.get(url)
.addQueryParameter(params)
.setTag("")
.setPriority(Priority.HIGH)
.build()
.getAsJSONArray(jsonArrayRequestListener);
}
public static void makeGetRequest(String url, Map<String, String> params, JSONObjectRequestListener jsonObjectRequestListener) {
Logger.d(params);
//AndroidNetworking.enableLogging();
AndroidNetworking.get(url)
.addQueryParameter(params)
.setTag("")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(jsonObjectRequestListener);
}
/**
* This make a GET request to the and uses the response listener map to an object
* @param url
* @param queryParams
* @param mClass
* @param parsedRequestListener
*/
public static void makeGetRequest(String url, Map<String, String> queryParams, Class mClass, ParsedRequestListener parsedRequestListener) {
Logger.d(queryParams);
//AndroidNetworking.enableLogging();
AndroidNetworking.get(url)
.addQueryParameter(queryParams)
.setTag(url)
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}
public static void makeGetRequest(String url, Map<String, String> pathParams, Map<String, String> queryParams, Class mClass, ParsedRequestListener<List> parsedRequestListener) {
AndroidNetworking.get(url)
.addPathParameter(pathParams)
.addQueryParameter(queryParams)
.setTag("")
.setPriority(Priority.MEDIUM)
.build()
.getAsObjectList(mClass, parsedRequestListener);
}
/**
* This make a GET request to the and uses the response listener map to an object
* @param url
* @param queryParams
* @param mClass
* @param parsedRequestListener
*/
public static void makePostRequest(String url, Map<String, String> queryParams, Class mClass, ParsedRequestListener parsedRequestListener) {
Log.e(TAG, "->->->->->->--- MakePostRequest Params (line:91) -------------------> ");
Logger.d(queryParams);
//AndroidNetworking.enableLogging();
AndroidNetworking.post(url)
.addQueryParameter(queryParams)
.setTag(url)
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}
public static void makeJSONPostRequest(String url, Map<String, Object> params, JSONObjectRequestListener jsonObjectRequestListener) {
String stringJson = new Gson().toJson(params);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(stringJson);
Log.e(TAG, jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
AndroidNetworking.post(url)
// .addBodyParameter(object)
.addJSONObjectBody(jsonObject)
.setTag("post")
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(jsonObjectRequestListener);
}
public static void makeJSONPostRequest(String url, Map<String, Object> params, Class mClass, ParsedRequestListener parsedRequestListener) {
String stringJson = new Gson().toJson(params);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(stringJson);
Log.e(TAG, jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
// AndroidNetworking.enableLogging();
AndroidNetworking.post(url)
// .addBodyParameter(object)
.addJSONObjectBody(jsonObject)
.setTag(url)
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}
public static void makeJSONPostRequest(String url, JSONObject jsonObjectParams, Class mClass, ParsedRequestListener parsedRequestListener) {
AndroidNetworking.post(url)
// .addBodyParameter(object)
.addJSONObjectBody(jsonObjectParams)
.setTag(url)
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}
public static void makePostRequest(String url, Object object, JSONArrayRequestListener jsonArrayRequestListener) {
AndroidNetworking.post(url)
.addBodyParameter(object)
.setTag("post")
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONArray(jsonArrayRequestListener);
}
/*public static void makePostRequest(String url, Object object, Class mClass, ParsedRequestListener parsedRequestListener) {
AndroidNetworking.enableLogging();
AndroidNetworking.post(url)
.addBodyParameter(object)
.setTag("post")
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}*/
public static void makeJSONPutRequest(String url, JSONObject jsonObjectParams, Class mClass, ParsedRequestListener parsedRequestListener) {
// AndroidNetworking.enableLogging(HttpLoggingInterceptor.Level.BODY);
AndroidNetworking.put(url)
.addJSONObjectBody(jsonObjectParams)
.setTag(url)
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(mClass, parsedRequestListener);
}
/**
* @deprecated
* @param anError fast android networking response error
*/
public static void handleError(ANError anError) {
if (anError.getErrorCode() != 0) { // received error from server
// error.getErrorCode() - the error code from server
// error.getErrorBody() - the error body from server
// error.getErrorDetail() - just an error detail
Log.e(TAG, "onError errorCode : " + anError.getErrorCode());
Log.e(TAG, "onError errorBody : " + anError.getErrorBody());
Log.e(TAG, "onError errorDetail : " + anError.getErrorDetail());
// get parsed error object (If ApiError is your class)
// ApiError apiError = error.getErrorAsObject(ApiError.class);
} else {
// Not Server Error
// error.getErrorDetail() : connectionError, parseError, requestCancelledError
Log.e(TAG, "onError errorDetail : " + anError.getErrorDetail());
}
}
public static void handleError(Context context, ANError anError) {
String TAG = "ANNetwork Error Handler";
if (anError.getErrorCode() != 0) {
// received error from server
// anError.getErrorCode() - the error code from server
// anError.getErrorBody() - the error body from server
// anError.getErrorDetail() - just an error detail
Log.e(TAG, "onError errorCode : " + anError.getErrorCode());
Log.e(TAG, "onError errorBody : " + anError.getErrorBody());
Log.e(TAG, "onError errorDetail : " + anError.getErrorDetail());
Log.e(TAG, "onError errorMessage : " + anError.getMessage());
Shortcuts.use(context).toastLong("Failed, Could Not Establish Your Request Please Try Again Later.");
// get parsed error object (If ApiError is your class)
// ApiError apiError = anError.getErrorAsObject(ApianError.class);
} else {
// anError.getErrorDetail() : connectionError, parseError, requestCancelledError
Log.e(TAG, "onError errorDetail : " + anError.getErrorDetail());
Log.e(TAG, "onError Message: " + anError.getMessage());
switch (anError.getErrorDetail()) {
case "connectionError":
Shortcuts.use(context).toastLong("Error Connecting, Please Check Your Internet Connection");
break;
case "parseError":
Shortcuts.use(context).toastLong("Oops! Could Not Process Your Request, Please Try Again");
break;
case "requestCancelledError":
Shortcuts.use(context).toastLong("Your Request Has Been Canceled..");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment