簡體   English   中英

如何在Retrofit2 Android中傳遞POST參數類型json?

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

我想在Retrofit2中傳遞類型為json的POST參數,如下所示!

{"person":{"phone":99999,"first_name":"fl","last_name":"asdad","email":"gb@fa.com", "home_address":{"country_code":"in","zip":123456}}}

我怎樣才能通過?

例如

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

作為參數,我的類型是json,其值在json字符串之上。 先感謝您。

這是我的完美答案。 我在找什么。

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

}

嘗試這個,

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

這是POJO類,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM