如何使用POST方法在android中使用Retrofit发送原始JSON

时间:2018-01-04 17:11:59

标签: android gson retrofit android-json

我想发布以下JSON,

{
  "name": {
          "firstName": "f_name",
          "lastName": "l_name"
  },
  "password": "mypassword123",
  "email": "test.mail@gmail.com"
}
接口内的

注册方法是,

@POST("user/createuser")
Call<RegisterResponseModel> register(@Body RegisterModel body);

使用寄存器方法,

Name name = new Name("f_name", "l_name");

RegisterModel registerModel = new RegisterModel(name, "mypassword123", "test.mail@gmail.com");

Call<RegisterResponseModel> res = apiService.register(registerModel);

但无法达到我想要的水平,请帮助我实现我的需求。提前致谢

3 个答案:

答案 0 :(得分:3)

你可以这样做,先创建像

这样的Pojo类
class SendDataModel{

    private String email;
    private Name name;
    private String password;

    public String getEmail ()
    {
        return email;
    }

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

    public Name getName ()
    {
        return name;
    }

    public void setName (Name name)
    {
        this.name = name;
    }

    public String getPassword ()
    {
        return password;
    }

    public void setPassword (String password)
    {
        this.password = password;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [email = "+email+", name = "+name+", password = "+password+"]";
    }
}

和其他Pojo类一样

class Name{
    private String lastName;

    private String firstName;

    public String getLastName ()
    {
        return lastName;
    }

    public void setLastName (String lastName)
    {
        this.lastName = lastName;
    }

    public String getFirstName ()
    {
        return firstName;
    }

    public void setFirstName (String firstName)
    {
        this.firstName = firstName;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [lastName = "+lastName+", firstName = "+firstName+"]";
    }
}

并设置您的名字和

Name name = new Name();
    name.setFirstName();
    name.setLastName();
SendDataModel sendDatamodel=new SendDataModel();
    sendDatamodel.setName(name);
    sendDatamodel.setEmail("yourEmail")
    sendDatamodel.setPassword("yourPassword"); 

并将sendDatamodel传递给您的请求。

Call<RegisterResponseModel> res = apiService.register(sendDatamodel);
res.enqueue(new Callback<RegisterResponseModel>() {
    @Override
    public void onResponse(Call<RegisterResponseModel> call, 
    Response<RegisterResponseModel> response) {

    }

    @Override
    public void onFailure(Call<RegisterResponseModel> call, Throwable t) 
    {
        // Log error here since request failed
        Log.e(TAG, t.toString());
    }
});

答案 1 :(得分:0)

您需要在res对象上调用enqueue函数。 See this example.

答案 2 :(得分:0)

试试这个

尝试POST data in raw

时,我遇到了同样的问题
  1. 您的API界面
  2. @POST("your_url_here")
    Call<Object> getUser(@Body Map<String, String> body);
    
    1. 致电class
    2. Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(Constants.BASE_URL)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
      ApiInterface apiInterface = retrofit.create(ApiInterface.class);
      
      try {
          Map<String, String> requestBody = new HashMap<>();
          requestBody.put("email", "abc@gmail.com");
          requestBody.put("password", "123678");
          Call<Object> call=apiInterface.getUser(requestBody);
          call.enqueue(new Callback<Object>() {
              @Override
              public void onResponse(Call<Object> call, Response<Object> response) {
                  try {
                      JSONObject object=new JSONObject(new Gson().toJson(response.body()));
                      Log.e("TAG", "onResponse: "+object );
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
              @Override
              public void onFailure(Call<Object> call, Throwable t) {
              }
          });
      } catch (Exception e) {
          e.printStackTrace();
      }