Android Retrofit 2.0 JSON文档未完全消耗。

4

我正在使用Retrofit 2.0通过POJO类以POST请求的形式发布数据,并返回字符串作为响应,无论是失败还是成功。现在的问题是它提示无法解析JSON。

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0

现在我正在尝试像下面这样发布jsonobect。 我的pojo类如下

  public class RecruiterProfileModel implements Serializable
    {
        @SerializedName("fname")
        private String firstname;
        @SerializedName("lname")
        private String lastname;
        @SerializedName("company")
        private String companyName;
        @SerializedName("address")
        private String address;
        @SerializedName("email")
        private String email;
        @SerializedName("paswd")
        private String password;
        @SerializedName("ph")
        private String phone;
        @SerializedName("location")
        private String location;
        @SerializedName("city")
        private String city;
        @SerializedName("state")
        private String state;
        @SerializedName("contry")
        private String contry;
        @SerializedName("pincode")
        private String pincode;
        @SerializedName("landmark")
        private String landmark;

        public RecruiterProfileModel(String fname, String lastname, String landmark, String location,
                                     String city, String state, String contry, String pincode,
                                     String email, String password, String phone,
                                     String address,String companyName)
        {
            this.firstname=fname;
            this.lastname=lastname;
            this.landmark=landmark;
            this.location=location;
            this.city=city;
            this.state=state;
            this.contry=contry;
            this.pincode=pincode;
            this.email=email;
            this.password=password;
            this.phone=phone;
            this.address=address;
            this.companyName=companyName;
        }

        public String getFirstname() {
            return firstname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getContry() {
            return contry;
        }

        public void setContry(String contry) {
            this.contry = contry;
        }

        public String getPincode() {
            return pincode;
        }

        public void setPincode(String pincode) {
            this.pincode = pincode;
        }

        public String getLandmark() {
            return landmark;
        }

        public void setLandmark(String landmark) {
            this.landmark = landmark;
        }
    }

我的接口使用POJO模型作为请求参数,并返回字符串响应,表示成功或失败。

@POST("empowerapp/providersreg.php")
    Call<String> registerRecruiter(
            @Body RecruiterProfileModel profileModel);

我用的POST请求方法

     public void registerRecruiter(RecruiterProfileModel profileModel) {
             Gson gson = new GsonBuilder().setLenient().create();

            OkHttpClient client = new OkHttpClient();

            Retrofit retrofit = new Retrofit.Builder().baseUrl(Allconstants.MAIN_URL).client(client).addConverterFactory(GsonConverterFactory.create(gson)).build();

            RetrofitInterface service = retrofit.create(RetrofitInterface.class);

            Call<String> call = service.registerRecruiter(profileModel);
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Response<String> response, Retrofit retrofit) {
                    System.out.println("###coming"+response.body().toString());
                    if (response.body().toString().equalsIgnoreCase("success"))
                    {
                        pd.dismiss();
                        loginSession.createLoginSession(Allconstants.RECRUITER,Allconstants.R_REG_ACTIVITY);
                        Toast.makeText(Registration.this,"successfully registered",Toast.LENGTH_LONG).show();
                        System.out.println("###coming"+response.body().toString());
                    }else{
                        pd.dismiss();
                        Toast.makeText(Registration.this,"oops!!!something went wrong..try again",Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    pd.dismiss();
                    Toast.makeText(Registration.this,t.getStackTrace().toString(),Toast.LENGTH_LONG).show();
                    System.out.println("###error1"+t.getMessage());
                }
            });
        }

现在的问题是它说无法解析json。请帮我解决这个问题。非常感谢您的帮助。提前致谢。


你能发一下你遇到的具体错误吗? - John O'Reilly
如果没有使用okhttp和gson,它会报错-com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 18 path $,但是当使用okhttp和gson时,它会说json文档未被完全消耗。 - md gouse
请帮我解决这个问题,我浪费了一整天的时间。 - md gouse
你能否也发布你收到的JSON数据(如果信息不是私密的话,可以附上测试数据)? - John O'Reilly
它会返回一个字符串作为响应,表示成功或失败。 - md gouse
显示剩余2条评论
2个回答

5

在创建 Retrofit 对象时,尝试添加 .addConverterFactory(ScalarsConverterFactory.create())

Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(Allconstants.MAIN_URL)
      .client(client)
      .addConverterFactory(ScalarsConverterFactory.create())
      .build();

你还需要将以下内容添加到你的build.gradle文件中(使用你指定的Retrofit版本):
   implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'

也许在那个版本中不可用...我正在使用compile "com.squareup.retrofit2:converter-scalars:2.1.0"...你能否更新你的依赖项以使用2.1版本? - John O'Reilly
我是否应该假设我能够传递你已经传递的内容,例如 OkHttpClient 实例? - John O'Reilly
无法解析编译'com.squareup.retrofit:converter-scalars:2.0.0'。 - md gouse
当添加2.1后,它现在显示无法将ScalarConvertor Factory.create作为参数传递,因为它是Retrofit而不是Retrofit2。 - md gouse
很奇怪,因为你明确将其作为retrofit2依赖项引入...你也更新了其他retrofit依赖项到2.1吗? - John O'Reilly
显示剩余4条评论

5
同样,我发现顺序也需要与ScalarsConverterFactory.create相同。
    .addConverterFactory(ScalarsConverterFactory.create()) //important
    .addConverterFactory(GsonConverterFactory.create(gson))

如果像以下这样将GSON放在顶部,它将无法工作:
    .addConverterFactory(GsonConverterFactory.create(gson))
    .addConverterFactory(ScalarsConverterFactory.create()) //important

1
添加依赖项 implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' 以使用 ScalarsConverterFactory。 - ASH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接