Dhiren Basra
Dhiren Basra

Reputation: 860

How to pass POST parameter type json in retrofit2 android,?

i want to pass POST parameter whose type is json in retrofit2 like given below !!

{"person":{"phone":99999,"first_name":"fl","last_name":"asdad","email":"[email protected]", "home_address":{"country_code":"in","zip":123456}}}

how do may i pass it.?

for example

@Headers({"Accept:application/json", "Content-Type:application/json;"})
@POST(WS.profile_new_user)
Call<ProfilePOJO> peopleRegister(@QueryMap Map<String, String> objectMap);

Call<ProfilePOJO> call = Api_Handler.getApiService().peopleRegister(getJsonString());

    call.enqueue(new Callback<ProfilePOJO>() {
        @Override
        public void onResponse(Call<ProfilePOJO> call, Response<ProfilePOJO> response) {
            if (response.isSuccessful()) {
                Toast.makeText(context, "done", Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();

            Log.e("response", new Gson().toJson(response.body()));

        }

        @Override
        public void onFailure(Call<ProfilePOJO> call, Throwable t) {

        }
    });
     public String getJsonString() {
        Map<String, String> map = new HashMap();
        map.add("",""); //i stuck @ here.i don't know how to pass json type as a parameter here.
        return map;
}

as a parameter my type is json whose value is above json String. Thank you in advance.

Upvotes: 2

Views: 5237

Answers (2)

Dhiren Basra
Dhiren Basra

Reputation: 860

this my perfect answer. what i looking for.

@Headers({"Accept:application/json", "Content-Type:application/json;"})
@POST(WS.profile_new_user)
Call<ProfilePOJO> peopleRegister(@Body RequestBody body);


Call<ProfilePOJO> call = Api_Handler.getApiService().peopleRegister(getJsonEncode());

    call.enqueue(new Callback<ProfilePOJO>() {
        @Override
        public void onResponse(Call<ProfilePOJO> call, Response<ProfilePOJO> response) {
            if (response.isSuccessful()) {
                Toast.makeText(context, "done", Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();

            Log.e("response", new Gson().toJson(response.body()));

        }

        @Override
        public void onFailure(Call<ProfilePOJO> call, Throwable t) {

        }
    });


private RequestBody getJsonEncode() {

    Map<String, Object> jsonMapHomeAddress = new ArrayMap<>();
    jsonMapHomeAddress.put("country_code", edt_country.getText().toString());
    jsonMapHomeAddress.put("zip", edt_zipcode.getText().toString());


    Map<String, Object> jsonArrayMap = new ArrayMap<>();
    jsonArrayMap.put("phone", edt_phone_no.getText().toString());
    jsonArrayMap.put("first_name", edt_fname.getText().toString());
    jsonArrayMap.put("last_name", edt_lname.getText().toString());
    jsonArrayMap.put("email", email);
    jsonArrayMap.put("home_address", jsonMapHomeAddress);

    Map<String, Object> jsonParams = new HashMap<>();
    jsonParams.put("person", jsonArrayMap);

    Log.e("params", jsonParams.toString());

    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), (new JSONObject(jsonParams)).toString());

    return body;

}

Upvotes: 4

shinil
shinil

Reputation: 1493

Try this,

public String getJsonString() {
    Map<String, String> map = new HashMap();

    HomeAddress homeAddress = new HomeAddress();
    homeAddress.setCountryCode("in");
    homeAddress.setZip(123456);

    Person person = new Person();
    person.setHomeAddress(homeAddress);
    person.setEmail("email");
    person.setFirstName("firstName");
    person.setLastName("lastName");
    person.setPhone(9999);

    Example example = new Example();
    example.setPerson(person);
    String pojo = example.toJson(); //this is your pojo as string. add it your map

    map.add("", pojo); type as a parameter here.
    return map;
}

Here are the POJO classes,

public class Example {

private Person person;
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public String toJson() {
    return GSON.toJson(this, Example.class);
  }

public Person getPerson() {
    return person;
  }

public void setPerson(Person person) {
    this.person = person;
  }
}



public class Person {

private Integer phone;
private String firstName;
private String lastName;
private String email;
private HomeAddress homeAddress;

public Integer getPhone() {
    return phone;
  }

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

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 getEmail() {
    return email;
  }

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

public HomeAddress getHomeAddress() {
    return homeAddress;
  }

public void setHomeAddress(HomeAddress homeAddress) {
    this.homeAddress = homeAddress;
  }
}


public class HomeAddress {

private String countryCode;
private Integer zip;

public String getCountryCode() {
    return countryCode;
  }

public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
  }

public Integer getZip() {
    return zip;
  }

public void setZip(Integer zip) {
    this.zip = zip;
  }
}

Upvotes: 2

Related Questions