I have an application that is a car parking system, my clients use it a lot to send to my database entries of data from the car.
here is the code to send to my server :
public void sendEntradaRotativaDataToServer(String url){
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Processando entrada..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
JsonObject json = new JsonObject();
json.addProperty("placa", entradaObj.getPlaca());
json.addProperty("dataemissao", entradaObj.getDataemissao());
json.addProperty("tipo", entradaObj.getTipo());
json.addProperty("valor", entradaObj.getValor());
json.addProperty("area", entradaObj.getArea().getId());
json.addProperty("observacao", entradaObj.getObservacao());
json.addProperty("user_id", entradaObj.getUser_id());
Ion.with(getActivity())
.load(url)
.setTimeout(5000)
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null) {
newMsgWarn("Atenção","Não foi possível fazer uma conexão por favor tente novamente");
if(pDialog != null && pDialog.isShowing()){
pDialog.dismiss();
}
} else {
success = result.get(TAG_SUCCESS).getAsInt();
message = result.get(TAG_MESSAGE).getAsString();
entradaObj.set_id(result.get("entradaid").getAsString());
(new SyncEntrada()).execute(new String[0]);
if(pDialog != null && pDialog.isShowing()){
pDialog.dismiss();
}
}
}
});
}
it is a simple post request with the Ion library and this is the code on the server ( i'm using laravel on the project ) :
public function insertDataRotativa(Request $request) {
if(isset($request)) {
$dataentradaini = Carbon::parse($request->input('dataemissao'));
$evento = $this->mdUtil->getEventByDataEmissao($dataentradaini);
if (!empty($evento)) {
foreach ($evento as $value) {
$this->mdEntrada = new Entrada;
$this->mdEntrada->placa = $request->input('placa');
$this->mdEntrada->created_at = $dataentradaini;
$this->mdEntrada->updated_at = $dataentradaini;
$this->mdEntrada->tipo = $request->input('tipo');
$this->mdEntrada->valor = $request->input('valor');
$this->mdEntrada->area = $request->input('area');
$this->mdEntrada->observacao = $request->input('observacao');
$this->mdEntrada->user_id = $request->input('user_id');
$this->mdEntrada->evento_id = $value->id;
$this->mdEntrada->flag = "U";
$this->mdEntrada->save();
}
}else {
$this->mdEntrada = new Entrada;
$this->mdEntrada->placa = $request->input('placa');
$this->mdEntrada->created_at = $dataentradaini;
$this->mdEntrada->updated_at = $dataentradaini;
$this->mdEntrada->tipo = $request->input('tipo');
$this->mdEntrada->valor = $request->input('valor');
$this->mdEntrada->area = $request->input('area');
$this->mdEntrada->observacao = $request->input('observacao');
$this->mdEntrada->user_id = $request->input('user_id');
$this->mdEntrada->flag = "U";
$this->mdEntrada->save();
}
$response["success"] = 1;
$response["message"] = "Entrada efetuada com sucesso.TIPO:1";
$response["entradaid"] = $this->mdEntrada->id;
return $response;
}
I'm guessing that is the user that press so many times the button to send but I put a time to enable and not enable on the button that will not make many requests in the same time, here is the code for the button :
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
YoYo.with(Techniques.BounceIn)
.duration(500)
.playOn(v.findViewById(R.id.entradabt));
if (SystemClock.elapsedRealtime() - mLastClickTime < 3000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
enviarEntrada();
}
});
Why am i having this kind of duplicate records? Any tips?
Here my schema
Unique_Key
Add a unique Key on Created_at, Updated_at and Placa Seeing as this might be the Unique identifier for this table. This prevent at exactly the same time insertions.
Also check if one request sends the SAME request multiple time and filter these out by PLACA (carplate?) (only 1 placa can be send in a single request).
Might be handy? If the same PLACA sends within 1 minute of paying the same request, ignore it. (SQL Query filter, request all transactions within last minute and check if (carplate)placa is there). result = SQL Query select all where placa is within 1 and or 2 min; if(result is same PLACA as request){ return; and ignore request)
Bit of pseudocode to help.