4

I am using retrofit 2.0 to post data in the form of post request with pojo class and returning string as an response either failure or success and now the problem is that it says json cannot be consumed.

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

Now I am trying to post jsonobect as like below. My pojo class is as below

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

My Interface is using pojo model as body parameter and returning string response either success or failure

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

My method for post request

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

The problem now is that it says json cannot be consumed.Please help me in resolving this issue .Your help will be much appreciated. Thanks in advance

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
md gouse
  • 527
  • 1
  • 6
  • 22
  • Can you post the specific error you're getting. – John O'Reilly Feb 22 '17 at 09:31
  • if there is no okhttp and gson it gives the error as -com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 18 path $ but when used okhttp and gson then it says json document was not fully consumed – md gouse Feb 22 '17 at 11:44
  • please help me out in resolving this issue.. wasted my entire day – md gouse Feb 22 '17 at 11:45
  • can you also post the json you're getting back (perhaps with test data if information is private). – John O'Reilly Feb 22 '17 at 11:49
  • it returns a string in response either success or failure – md gouse Feb 22 '17 at 11:53
  • Retrofit is expecting a JSON response and you are getting a plain text response. You should provide a response like {"result":"success"} instead of a plain "success". – AndroidRuntimeException Feb 22 '17 at 12:44
  • its expecting string as response as like call.enqueue(new Callback() { @Override public void onResponse(Response response, Retrofit retrofit) { System.out.println("###coming"+response.body().toString()); – md gouse Feb 22 '17 at 13:14

2 Answers2

5

try adding .addConverterFactory(ScalarsConverterFactory.create()) when creating Retrofit object.

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

You'll also need to add following to your build.gradle (using version of retrofit you indicated you were using)

   implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
ASH
  • 75
  • 1
  • 12
John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
5

Also I found out that order also needs to be same with ScalarsConverterFactory.create

    .addConverterFactory(ScalarsConverterFactory.create()) //important
    .addConverterFactory(GsonConverterFactory.create(gson))

It wont work if GSON is on top like following :

    .addConverterFactory(GsonConverterFactory.create(gson))
    .addConverterFactory(ScalarsConverterFactory.create()) //important
NehaK
  • 2,639
  • 1
  • 15
  • 31
  • 1
    Add dependency implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' for ScalarsConverterFactory – ASH Nov 07 '20 at 13:46