Error 400, Bad Request, Json Parse error: Unrecognized token 'agama' : was Expecting ('true','false', or 'null') in Android Retrofit with Spring Boot

只谈情不闲聊 提交于 2020-04-30 06:26:21

问题


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


回答1:


"agama":
 {
    "id_Agama": "1"
 }

but agama is just a string so solve it like this

"agama":"1"




回答2:


set your value like this.

 JSONObject jobj = new JSONObject();
    try {
      jobj.put("id_Agama", "1");
    } catch (JSONException e) {
      e.printStackTrace();
    }

pass in your request like this.

@Field("agama") JSONObject jobj,



回答3:


interface class:

  @FormUrlEncoded
    @POST("http://103.82.240.144:1111/WSHRIS/pegawai/family/add")
    Call<familylistresponse> addFams(@Header("Content-Type") String content_type,
                                     @Header("Authorization") String auth,
                                     @Query("id") String id,
                                     @Field("noPegawai") boolean noPegawai,
                                     @Field("agama") JSONObject  agama,
                                     @Field("hubungans") JSONObject hubungans,
                                     @Field("jeniskelamin") JSONObject jeniskelamin,
                                     @Field("tgl_Lahir") String tgl_lahir,
                                     @Field("nama") String nama,
                                     @Field("keterangan") String keterangan,
                                     @Field("tempatLahir") String tempatLahir,
                                     @Field("golDarah") String goldar,
                                     @Field("statusNikah") String statusNikah);

Pass in argument JSON object in argument

    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);

 JSONObject agamajsonObject= new JSONObject();
 JSONObject hubungansjsonObject= new JSONObject();
 JSONObject jeniskelaminjsonObject= new JSONObject();

        try {
          agamajsonObject.put("id_Agama", "1");
          hubungansjsonObject.put("id", "10");
          jeniskelaminjsonObject.put("jenisKelamin", "1");
        } catch (JSONException e) {
          e.printStackTrace();
        }

            Call<familylistresponse> call = apiService.addFams("application/json","Bearer " + tokens , id, true, agamajsonObject, hubungansjsonObject, jeniskelaminjsonObject, tgl_Lahir, nama, keterangan, tempatLahir, golDarah,statusNikah);
            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());
                }
            });
        }



回答4:


I've RESOLVED 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!



来源:https://stackoverflow.com/questions/61386053/error-400-bad-request-json-parse-error-unrecognized-token-agama-was-expec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!