Skip to content

Instantly share code, notes, and snippets.

@LuiguiBalarezo
Last active October 22, 2018 19:53
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 LuiguiBalarezo/58b40c4b9955915ca44b46bb5636e004 to your computer and use it in GitHub Desktop.
Save LuiguiBalarezo/58b40c4b9955915ca44b46bb5636e004 to your computer and use it in GitHub Desktop.
package com.zeta.scriptgo.zentinela.base;
import android.content.Context;
import android.widget.Toast;
import com.zeta.scriptgo.zentinela.HttpCallBacks.HttpInterfaceCallBack;
import com.zeta.scriptgo.zentinela.Utils.ObjectResult;
import com.zeta.scriptgo.zentinela.Utils.RealmManager;
import com.zeta.scriptgo.zentinela.constans.ConstansHelps;
import com.zeta.scriptgo.zentinela.dialogs.DialogIndeterminate;
import com.zeta.scriptgo.zentinela.models.M_API;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by lbalarezo on 17/03/2018.
*/
public class BaseService {
public HttpInterfaceCallBack callback = null;
protected String TAG = this.getClass().getSimpleName();
protected M_API m_api = null;
protected Context context;
protected String responsestatusconnection;
protected int codehttpresponse, typeresposestatusconnection;
protected JSONObject jsonObject;
protected int error;
protected String messageerror;
protected JSONArray resultjsonarray;
protected Integer items;
protected String message;
protected Integer status;
protected ObjectResult objectResult;
private String typedata = null;
private static BaseService dialoginstance = null;
public static BaseService getInstance() {
if (dialoginstance == null) {
dialoginstance = new BaseService();
}
return dialoginstance;
}
protected String getResponseStatusConnection(String errorMessage, String responsestatusconnection) {
if (responsestatusconnection == null || responsestatusconnection.equals("")) {
if (errorMessage != "") {
if (errorMessage.contains(" ENETUNREACH ")) {
errorMessage = "No tiene salida a Internet!.";
} else if (errorMessage.contains(" ECONNREFUSED ")) {
errorMessage = "El servidor no responde!";
} else if (errorMessage.contains(" ECONNRESET ")) {
errorMessage = "El Operador bloqueo la peticion!";
typeresposestatusconnection = ConstansHelps.INFO;
} else if (errorMessage.contains(" ETIMEDOUT ")) {
errorMessage = "Lentitud de respuesta!";
} else if (errorMessage.contains(" EHOSTUNREACH ")) {
errorMessage = "No conecto a la ruta!";
} else {
errorMessage = "Sin Conexion al servidor!";
}
}
return errorMessage;
} else {
return responsestatusconnection;
}
}
protected boolean notEmpty(int codehttpresponse) {
boolean isfull = true;
switch (codehttpresponse) {
case 0:
isfull = false;
callback.responseStatus(responsestatusconnection, ConstansHelps.ERROR);
break;
case 200:
switch (typedata) {
case ConstansHelps.OBJECT:
if (objectResult.resultObject.length() == 0) {
callback.emtpy();
isfull = false;
} else {
isfull = true;
break;
}
break;
case ConstansHelps.ARRAY:
if (objectResult.resultArray.length() == 0) {
callback.emtpy();
isfull = false;
} else {
isfull = true;
break;
}
break;
default:
Toast.makeText(context, TAG + " > notEmpty() | NOT PROCCESS TYPE OBJECT", Toast.LENGTH_SHORT).show();
break;
}
}
return isfull;
}
protected ObjectResult getResultResponse(String stringresult, String typedata) {
try {
objectResult = new ObjectResult();
jsonObject = new JSONObject(stringresult);
objectResult.existerror = jsonObject.getInt(ConstansHelps.JERROR);
objectResult.messageerror = jsonObject.getString(ConstansHelps.JERRORMSG);
//DEPENDE DEL PROGRAMADOR QUE TIPO DE JSON USAR (OBJECT O ARRAY)
this.typedata = typedata;
switch (typedata) {
case ConstansHelps.OBJECT:
objectResult.resultObject = jsonObject.getJSONObject(ConstansHelps.JDATA);
break;
case ConstansHelps.ARRAY:
objectResult.resultArray = jsonObject.getJSONArray(ConstansHelps.JDATA);
break;
default:
Toast.makeText(context, TAG + " > getResultResponse() | NOT PROCCESS TYPE OBJECT", Toast.LENGTH_SHORT).show();
break;
}
objectResult.idestatus = jsonObject.getInt(ConstansHelps.JSTATUS);
objectResult.message = jsonObject.getString(ConstansHelps.jMSG);
} catch (JSONException e) {
callback.error(e.getMessage());
e.printStackTrace();
}
return objectResult;
}
protected ObjectResult getProcessStringResult(String stringResults, String typedata) {
objectResult = null;
if (stringResults != null) {
if (stringResults != "") {
objectResult = getResultResponse(stringResults, typedata);
} else {
responsestatusconnection = ConstansHelps.SERVERNOTFOUND;
typeresposestatusconnection = ConstansHelps.ERROR;
}
} else {
responsestatusconnection = ConstansHelps.SERVERNOTFOUND;
typeresposestatusconnection = ConstansHelps.ERROR;
}
return objectResult;
}
protected void setResponseStatus(String response) {
typeresposestatusconnection = ConstansHelps.ERROR;
responsestatusconnection = getResponseStatusConnection(response, responsestatusconnection);
}
protected void runprocess(String s) {
if (context != null) {
Toast.makeText(context, "En Proceso " + s + "...", Toast.LENGTH_SHORT).show();
}
}
protected boolean validateConnection() {
m_api = RealmManager.using(TAG).where(M_API.class).findFirst();
if (m_api == null) {
Toast.makeText(context, "FALTA LA RUTA DE CONEXION!.", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
public void cancelAll() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment