Skip to content

Instantly share code, notes, and snippets.

@LuiguiBalarezo
Created October 26, 2018 20:32
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/302c0ab8e53a8ab357f0b6e824184498 to your computer and use it in GitHub Desktop.
Save LuiguiBalarezo/302c0ab8e53a8ab357f0b6e824184498 to your computer and use it in GitHub Desktop.
package com.zeta.scriptgo.zentinela.Async;
import android.os.AsyncTask;
import com.zeta.scriptgo.zentinela.models.Guia_M;
import com.zeta.scriptgo.zentinela.models.Chofer_M;
import com.zeta.scriptgo.zentinela.models.Unidad_M;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import io.realm.RealmList;
/**
* Created by lbalarezo on 26/10/2018.
*/
public class AsyncGuias extends AsyncTask<JSONArray, Void, RealmList<Guia_M>> {
String TAG = this.getClass().getSimpleName();
Guia_M guia_m = null;
JSONObject jsonObject = null;
RealmList<Guia_M> guias = null;
Integer idguia = 0, estado, idchofer = 0;
String placa = null, licencia = null;
SimpleDateFormat simpleDateFormat_mixto = null, simpleDateFormat = null;
String fechaexpiracionrevisiontecnica, fechaexpiracioncapacitacion, fechaexpiracionlicencia;
CallBack callBack;
boolean existserror = false;
public AsyncGuias(CallBack call) {
super();
callBack = call;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
guias = new RealmList<>();
simpleDateFormat_mixto = new SimpleDateFormat("dd/MM/yyyy");
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
}
@Override
protected RealmList<Guia_M> doInBackground(JSONArray... params) {
try {
for (int i = 0; i < params[0].length(); i++) {
jsonObject = params[0].getJSONObject(i);
guia_m = new Guia_M();
guia_m.id = jsonObject.getInt("id");
guia_m.serie = Integer.parseInt(jsonObject.getString("serie"));
guia_m.correlativo = Integer.parseInt(jsonObject.getString("correlativo"));
guia_m.b10f = jsonObject.optInt("b10f", 0);
guia_m.b10p = jsonObject.optInt("b10p", 0);
guia_m.b45 = jsonObject.optInt("b45", 0);
guia_m.b10r = jsonObject.optInt("b10fu", 0);
guia_m.b5r = jsonObject.optInt("b10pu", 0);
guia_m.b15r = jsonObject.optInt("b45u", 0);
guia_m.fechacreacion = simpleDateFormat.parse(jsonObject.getString("fechacreacion"));
guia_m.fechatraslado = simpleDateFormat.parse(jsonObject.getString("fechatraslado"));
guia_m.estado = jsonObject.getInt("estado");
placa = jsonObject.getString("placa");
if (placa != null) {
guia_m.unidad = new Unidad_M();
guia_m.unidad.placa = placa;
guia_m.unidad.tipounidad = jsonObject.optInt("tipounidad", 0);
guia_m.unidad.descripcionunidad = jsonObject.optString("descripcionunidad", "");
guia_m.unidad.autoconsumo = jsonObject.optInt("autoconsumo", 0);
String gps = jsonObject.getString("gps");
if (gps != null) {
guia_m.unidad.gps = gps;
}
guia_m.unidad.capacidad = String.valueOf(jsonObject.optInt("capacidad", 0));
guia_m.unidad.idzonal = jsonObject.optInt("idzonal", 0);
fechaexpiracionrevisiontecnica = jsonObject.optString("fechaexpiracionrevisiontecnica");
if (fechaexpiracionrevisiontecnica != null && !fechaexpiracionrevisiontecnica.equals("null")) {
guia_m.unidad.fechaexpiracionrevisiontecnica = simpleDateFormat_mixto.parse(fechaexpiracionrevisiontecnica);
guia_m.unidad.diasexpiracionrevisiontecnica = jsonObject.optInt("diasexpiracionrevisiontecnica", 0);
}
}
licencia = jsonObject.getString("licencia");
if (licencia != null) {
guia_m.licencia = licencia;
}
idchofer = jsonObject.optInt("idchofer", 0);
if (!idchofer.equals(0)) {
guia_m.chofer = new Chofer_M();
guia_m.chofer.id = jsonObject.optInt("idchofer", 0);
guia_m.chofer.licencia = jsonObject.optString("licencia");
guia_m.chofer.dni = jsonObject.optString("dnichofer", "");
guia_m.chofer.nombre = jsonObject.optString("nombre", "");
guia_m.chofer.apepat = jsonObject.optString("apepat", "");
guia_m.chofer.apemat = jsonObject.optString("apemat", "");
guia_m.chofer.tipo = "chofer";
guia_m.chofer.supervisor = jsonObject.optString("nombresupervisor", "Sin Registro");
fechaexpiracioncapacitacion = jsonObject.optString("fechaexpiracioncapacitacion", null);
if (fechaexpiracioncapacitacion != null && !fechaexpiracioncapacitacion.equals("null")) {
guia_m.chofer.fechaexpiracioncapacitacion = simpleDateFormat_mixto.parse(fechaexpiracioncapacitacion);
guia_m.chofer.diasexpiracioncapacitacion = jsonObject.optInt("diasexpiracion", 0);
}
fechaexpiracionlicencia = jsonObject.optString("fechaexpiracionlicencia", null);
if (fechaexpiracionlicencia != null && !fechaexpiracionlicencia.equals("null")) {
guia_m.chofer.fechaexpiracionlicencia = simpleDateFormat_mixto.parse(fechaexpiracionlicencia);
guia_m.chofer.diasexpiracionlicencia = jsonObject.optInt("diasexpiracionlicencia", 0);
}
}
guias.add(guia_m);
}
} catch (JSONException e) {
callBack.error(e.getMessage());
existserror = true;
} finally {
return guias;
}
}
@Override
protected void onPostExecute(RealmList<Guia_M> guia_ms) {
super.onPostExecute(guia_ms);
if (!existserror) {
callBack.success(guia_ms);
}
}
public interface CallBack {
void success(RealmList<Guia_M> guia_ms);
void error(String e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment