错误400,错误请求,Json Parse错误:无法识别的令牌"agama":在使用Spring Boot进行Android改造的情况下是预期的("true","false"或"null") [英] Error 400, Bad Request, Json Parse error: Unrecognized token 'agama' : was Expecting ('true','false', or 'null') in Android Retrofit with Spring Boot

查看:168
本文介绍了错误400,错误请求,Json Parse错误:无法识别的令牌"agama":在使用Spring Boot进行Android改造的情况下是预期的("true","false"或"null")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种JSON: 已编辑

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"
        }
}

我有这个接口类:

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

我想将数据存储到数据库服务器,所以我在button.setOnClickListener中添加此方法:

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 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.接口类:我只是将表单格式更改为@Body类型,我认为这使我更容易实际插入数据,您只需要按顺序对输入类型值进行排序,以便将其自动插入到您的服务器API输入格式.

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.嵌套对象:因此,在此之前,我已经尝试了你们这些天给我的一些解决方案,并为此表示感谢.但是我为解决自己的问题所做的事情却有所不同.如您所见,我在agamas,hubungan和jeniskelamin上嵌套了对象.我做了什么这只是一个简单的过程,就像这样(称呼您的"GSON生成类"):

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

在那之后,您只需要在Activity中将这些变量添加到您的Main Model类中即可:

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....

还有BOOM!你去〜 这是另外一个提示:确保客户端应用程序上的变量与数据库服务器上的变量相同! :D你好~~ 如果您认为此答案有帮助,请对其投票!

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!

这篇关于错误400,错误请求,Json Parse错误:无法识别的令牌"agama":在使用Spring Boot进行Android改造的情况下是预期的("true","false"或"null")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆