2017-05-29 201 views
0

我想通过POST参数的类型是json在retrofit2中就像下面给出的!如何在retrofit2 android中传递POST参数类型json,?

{"person":{"phone":99999,"first_name":"fl","last_name":"asdad","email":"[email protected]", "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字符串。 预先感谢您。

+0

的可能的复制[如何在改造请求的主体立柱鲜全脂JSON?](https://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-请求) – moritzg

回答

1

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

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

} 
2

试试这个,

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

感谢哥们的答案,但我刚刚做了,另一种方式,我会张贴我自己的答案,你可能会检查它。 – Dhiren

+0

我把时间花在你的问题上,你至少可以投票。谢谢 –

相关问题