Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use POST method to send raw JSON using Retrofit in android

I want to post following JSON,

{
  "name": {
          "firstName": "f_name",
          "lastName": "l_name"
  },
  "password": "mypassword123",
  "email": "[email protected]"
}

register method inside Interface is,

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

using register method like,

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

RegisterModel registerModel = new RegisterModel(name, "mypassword123", "[email protected]");

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

but couldn't reach what I want, Please help me to achieve what I need. Thanks in advance

like image 761
Kavin-K Avatar asked Jan 04 '18 17:01

Kavin-K


2 Answers

You can do like this first create a Pojo class like

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+"]";
    }
}

and other Pojo Class like

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+"]";
    }
}

and set your first and last name like

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

and pass your sendDatamodel to your request.

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());
    }
});
like image 78
umesh shakya Avatar answered Sep 30 '22 16:09

umesh shakya


try this

I was facing the same problem when trying to POST data in raw form only

  1. Your API interface

@POST("your_url_here")
Call<Object> getUser(@Body Map<String, String> body);

  1. Call in your class

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", "[email protected]");
    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();
}
like image 21
Vishal Yadav Avatar answered Sep 30 '22 18:09

Vishal Yadav