0

Kindly read this question, that is asked by me before some hours: How is it possible that POST API working in Postman but not the retrofit?

Actually, from Retrofit response, I'm getting response in String format from ResponseBody object.

Explanation: My status code is 200, successful, but from the response body, it is only String. How can I get all data from that String?

After login success, I'm getting String in onResponse. I'm already sending user credential to this APi and in API response, I'm getting this String.

May be there is something encode decode with base64 relation.

Pooja Singh
  • 149
  • 2
  • 10

1 Answers1

0
public class LoginModel {

    @SerializedName("body")
    private Body body;

    public Body getBody() {
        return body;
    }

    public static class Body {
        @SerializedName("userPwd")
        private String userpwd;
        @SerializedName("prodId")
        private int prodid;
        @SerializedName("emailId")
        private String emailid;
        @SerializedName("customerId")
        private int customerid;

        public String getUserpwd() {
            return userpwd;
        }

        public int getProdid() {
            return prodid;
        }

        public String getEmailid() {
            return emailid;
        }

        public int getCustomerid() {
            return customerid;
        }
    }
}

Now in if you followed this answer you retrofit interface is something like this

@Headers("Content-Type: application/json")
@POST("login/desktop/user")
Call<ResponseBody> getToken(@Body HashMap<String, HashMap<String, Object>> data);

change it to

@Headers("Content-Type: application/json")
@POST("login/desktop/user")
Call<LoginModel> getToken(@Body HashMap<String, HashMap<String, Object>> data); // change here

Now you can get your data in onResponse() like this

LoginModel loginData = response.body();

String pswd = loginData.getBody().getUserpwd();

PS = if you can change the response I suggest you to change var body from response to something else like data.

SerializedName used in POJO class are not important, read more about them here

Hope this will help!

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29