0
{

    "first_name": "yy",
    "last_name": "uu",
    "mobile": "123456",
    "email": "qwerty32@g.com",
    "user_type":"user",
    "bio": "hell",
    "expertise_json": {"0":"1", "1":"2"}

}

This is the Josn i have to send, My java classe for this user:

public class User {
    @SerializedName("email")
    private String userEmail;
    @SerializedName("password")
    private String userPassword;
    @SerializedName("first_name")
    private String userFirstName;
    @SerializedName("last_name")
    private String userLastName;
    @SerializedName("mobile")
    private String userMobile;
    @SerializedName("user_type")
    private String userType;

    @SerializedName("expertise_json")
    @Expose
    private HashMap<String,String> userExpertise;

    @SerializedName("bio")
    private String bio;
   }

public interface ApiInterface {

    @POST("register")
    Call<User> createUser(@Body User user);
    @GET("init")
    Call<Expertises> getExpertise();

}

everything is saving to server except expertise_json. Also expertise_json is dynamic not limited to 2 or 3 values but can be any 10 20 etc. Any help will be highly appreciated. Thank you

Danish
  • 51
  • 12

3 Answers3

1

Replace your User class with this:

public class User {
@SerializedName("email")
private String userEmail;
@SerializedName("password")
private String userPassword;
@SerializedName("first_name")
private String userFirstName;
@SerializedName("last_name")
private String userLastName;
@SerializedName("mobile")
private String userMobile;
@SerializedName("user_type")
private String userType;

@SerializedName("expertise")
@Expose
private HashMap<String,String> userExpertise;

@SerializedName("bio")
private String bio;

}

0

Try this way :

@SerializedName("expertise")
@Expose
private Expertise expertise;

Expertise.java

public class Expertise {

@SerializedName("0")
@Expose
private String _0;
@SerializedName("1")
@Expose
private String _1;

public String get0() {
return _0;
}

public void set0(String _0) {
this._0 = _0;
}

public String get1() {
return _1;
}

public void set1(String _1) {
this._1 = _1;
}
Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
0

I see "@SerializedName" in you code, so gson should be used.
Here is a link saying what is the basic purpose of @SerializedName annotation.

what is the basic purpose of @SerializedName annotation in android using Gson

So, when you use @SerializedName, that means you specify the value of a field using the value of a specific tag in json. For your case, which means the field userExpertise using the value of expertise_json in json, which does not existed at all. The solution can be changing expertise_json into expertise

public class User {

  @SerializedName("email")       private String userEmail;
  @SerializedName("password")    private String userPassword;
  @SerializedName("first_name")  private String userFirstName;
  @SerializedName("last_name")   private String userLastName;
  @SerializedName("mobile")      private String userMobile;
  @SerializedName("user_type")   private String userType;

  @SerializedName("expertise") 
  @Expose
  private HashMap<String,String> userExpertise;

  @SerializedName("bio") private String bio;

}

ADD:
Here: In you code.

public interface ApiInterface {

  @POST("register")
  Call<User> createUser(@Body User user);  // watch this param, 'User user'

  ...

Now, let's go back to the json file

{
  "status": true,
  "body": {
     "first_name": "yy",
     ...
     "expertise_json": {"0":"1", "1":"2"}
  }
}

Here is the problem:
You are sending a body, whose content is a User, and as a User, it has the field of userEmail, userType and etc, but do not has the field of status, do not has the field of body.

So you wanna gson works, you should provide a class that contains these fields.

public class User {
    @SerializedName("status")    private boolean status;
    @SerializedName("body")      private Body body;

    public static class Body {
        @SerializedName("email")       private String userEmail;
        @SerializedName("password")    private String userPassword;
        @SerializedName("first_name")  private String userFirstName;
        @SerializedName("last_name")   private String userLastName;
        @SerializedName("mobile")      private String userMobile;
        @SerializedName("user_type")   private String userType;

        @SerializedName("expertise") 
        @Expose
        private HashMap<String,String> userExpertise;

        @SerializedName("bio") private String bio;
    }
 }

It should work now.

Community
  • 1
  • 1
Chris Wong
  • 451
  • 3
  • 5
  • I replaced expertise_json to expertise but still not working, I need to use HashMap for expertise because these values expertise_json are dynamic can be more then 2 like 5 or 10 etc – Danish Dec 02 '16 at 09:02
  • I have used gson to decode your json, and the **userExpertise** has 2 key-value. – Chris Wong Dec 02 '16 at 09:52
  • No expertise still not going to database, everything else is going – Danish Dec 02 '16 at 12:30