In my Android application, I try to send a List<MyClass> list
through Retrofit to my server database. Achieved some web services and scripts but with in array request I need a hand. I need to know how to send request from postman and handle it wth script (node.sj)
- This is my database table (
kontrol
) atphpAdmin
side
ogr_id
tur_id
and durum_id
are fields of my class object.
What I have tried is as script of it
var express = require('express'); var utils = require('./custom_utils.js');
module.exports = function(database){ var router = express.Router();
router.post('/', function(req, res, next) {
var nesne = "merhaba";
database.query('INSERT INTO kontrol (ogr_id, tur_id, durum_id,tarih) VALUES ? ',
[Array.from(req.body.ogrenciler).map(function(g){ return [g, nesne, 0]; })], function(err, results, fields){
var n = results.length;
if(err){
console.log(err);
res.json({ status: utils.respondMSG.DB_ERROR });
}else if(n<1){
res.json({status: utils.respondMSG.DB_ERROR});
}else {
res.json({status: utils.respondMSG.SUCCEED});
}
});
});
return router;
};
- Here is my Postman Request
I am able to use Retrofit Call back serives due to postman result. What I am trying to do is sent a correct request and write a script to handle it at server side
{ "ogrenciler" : [ { "ogr_id" : 17, "tur_id" : 1, "durum_id": 2, "tarih":"2017-17-02" }, {"ogr_id" : 74, "tur_id" : 1, "durum_id": 1, "tarih":"2017-17-02" } ] }
As I told it ogr_id
tur_id
durum_id
are fields of my List Object, also would like to know if there is an more efficent request and of course its scirpt side. At Least I hope to hear some solutions to my first try
************************* SOLVED - EDITED *************************
- What I am trying to send from Postman like
- Retrofit service method is
{
@POST("saveOgrYoklama")
Call<YoklamaStatus> sendYoklamaList2(@Body Yoklama yoklama);
}
- Model Classes to handle the values
{
public class Yoklama implements Serializable {
@SerializedName("tarih")
private String tarih;
@SerializedName("ogrenciler")
private ArrayList<YoklamaOgrenciler> ogrenciler;
public class YoklamaOgrenciler implements Serializable {
@SerializedName("ogr_id")
private int ogr_id;
@SerializedName("tur_id")
private int tur_id;
@SerializedName("durum_id")
private int durum_id;
}
}
}
- And Script of it
var express = require('express');
var utils = require('./custom_utils.js');
module.exports = function(database){
var router = express.Router();
router.post('/', function(req, res, next) {
var jsondata = req.body.ogrenciler;
var values = [];
//var mysqlTimestamp = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
var st = req.body.tarih;
//console.log(st);
var uzunluk = req.body.ogrenciler.length;
for(var i=0; i< uzunluk; i++)
values.push([ jsondata[i].ogr_id, jsondata[i].tur_id, jsondata[i].durum_id,st ]);
console.log(req.body.ogrenciler);
console.log(values);
console.log(jsondata);
console.log("uzunluk is :"+uzunluk);
console.log("Deneme asds");
database.query('INSERT INTO kontrol (ogr_id, tur_id, durum_id, tarih) VALUES ? ', [values], function(err, results, fields){
var n = results.length;
console.log(results);
console.log("Affected Row Num : "+results.affectedRows);
if(err){
console.log(err);
res.json({ status: utils.respondMSG.DB_ERROR });
}else if(n==NaN){
console.log("result NaN");
res.json({status: utils.respondMSG.SUCCEED});
} else if(n==null){
console.log("result null");
res.json({status: utils.respondMSG.SUCCEED});
}else if(n<1){
console.log("result < 1 ");
res.json({status: utils.respondMSG.SUCCEED});
}else {
res.json({status: utils.respondMSG.SUCCEED});
}
/**/
});
});
return router;
};
Be sure that you dont have @FormUrlEncoded above your service method. Babanız bu iyiliği yapmaz söyliim. Hope it will help others, lost many days on it