I have this kind of JSON: EDITED
{
"tgl_Lahir": "1960-12-18 00:00:00",
"nama": "Rahmi P",
"keterangan": "HIDUP",
"tempatLahir": "YOGYAKARTA",
"noPegawai": "010713",
"golDarah": "0",
"statusNikah": "0",
"hubungans": {
"id": "10"
},
"agama": {
"id_Agama": "1"
},
"jeniskelamin": {
"jenisKelamin": "1"
}
}
I have this interface class:
@FormUrlEncoded
@POST("http://ipaddress/family/add")
Call<familylistresponse> addFams(@Header("Content-Type") String content_type,
@Header("Authorization") String auth,
@Query("id") String id,
// @Field("noPegawai") JSONObject noPegawai,
@Field("agama") JSONObject agama,
@Field("hubungans") JSONObject hubungans,
@Field("jeniskelamin") JSONObject jeniskelamin,
@Field("tgl_Lahir") JSONObject tgl_lahir,
@Field("nama") JSONObject nama,
@Field("keterangan") JSONObject keterangan,
@Field("tempatLahir") JSONObject tempatLahir,
@Field("golDarah") JSONObject goldar,
@Field("statusNikah") JSONObject statusNikah);
I want to store my data to database server so I add this method inside my button.setOnClickListener:
public void addFamily(String noPegawai, String agama, String hubungan, String jenisKelamins, String tgl_Lahir, String nama, String keterangan, String tempatLahir, String golDarah, String statusNikah){
String id=null;
SharedPreferences preferences = getSharedPreferences("MyPref",0);
String tokens = preferences.getString("userToken",null);
try {
jsonObject.put("noPegawai", noPegawai);
jsonObject.put("agama", agama);
jsonObject.put("hubungans", hubungan);
jsonObject.put("jeniskelamin", jenisKelamins);
jsonObject.put("tgl_Lahir", tgl_Lahir);
jsonObject.put("nama", nama);
jsonObject.put("keterangan", keterangan);
jsonObject.put("tempatLahir", tempatLahir);
jsonObject.put("golDarah", golDarah);
jsonObject.put("statusNikah", statusNikah);
} catch (JSONException e) {
e.printStackTrace();
}
Call<familylistresponse> call = apiService.addFams("application/json","Bearer " + tokens , id, jsonObject, jsonObject, jsonObject, jsonObject , jsonObject, jsonObject, jsonObject, jsonObject, jsonObject);
call.enqueue(new Callback<familylistresponse>() {
@Override
public void onResponse(Call<familylistresponse> call, Response<familylistresponse> response) {
try {
if(response.body()!=null)
Toast.makeText(TambahDataKeluarga.this," response message success "+response.body(),Toast.LENGTH_LONG).show();
if(response.errorBody()!=null)
Toast.makeText(TambahDataKeluarga.this," response message error "+response.errorBody().string(),Toast.LENGTH_LONG).show();
Log.e("ERROR: ", response.errorBody().string());
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(TambahDataKeluarga.this," Tokens "+tokens,Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<familylistresponse> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
}
but when I try to post it to the server it shows me this error
Any ideas? or it's because of my nested son? or my interface's class? Thank you
I've SOLVED THE ISSUE! I don't know if this will become a solution for future readers or not. But at least it'll help a little bit.
1. Interface Class: I just change the form format into @Body type, I think it makes me easier to actually insert the data, you just have to sort your input type value in sequence so that it will automatically be inserted to your server API input format.
@Headers({ "Content-Type: application/json;charset=UTF-8"})
@POST("http://ipaddress/family/add/")
Call<familylistresponse> addFams(@Header("Authorization") String auth,
@Body familylistresponse familybody);
2. Nested Objects: So before, I've tried some solutions that you guys gave to me these days, and thank you for that. But what I did to solve my issue was different. So as you can see that I have nested objects on agamas, hubungans, and jeniskelamin. What did I do? It's just a simple one, like this (Call your "GSON Generate Class") like this :
Agama agamas = new Agama();
agamas.setIdAgama(vAgama);
Jeniskelamin jks = new Jeniskelamin();
jks.setJenisKelamin(vJenisk);
Hubungans hubungans = new Hubungans();
hubungans.setId(vHub);
After that, you just need to add those variables into your Main Model Class in your Activity :
familylistresponse f = new familylistresponse();
f.setTglLahir(name);
f.setAgama(agamas);
f.setHubungan(hubungans);
etc....
And BOOM! There you go~ This is an additional TIPS: Make sure your variables on the client's app are the same as you've settled on your database server! :D Ur Welcome ~~ Please vote for this answer if you feel that it is helpful!