Yusuf Önder
Yusuf Önder

Reputation: 21

Retrofit parameters go empty to server

I am using retrofit library for post request. But I'm getting empty field area error from server. So my parameters("signin[password] and signin[username]") go empty to server. Where is my mistake ? Thank you

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                Interfacem service = retrofit.create(Interfacem.class);
                SignBody body=new SignBody("username","pass");

                Call<ResponseBody> result =service.getResponse(body);


                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {

                            System.out.println(response.body().string().toString());
                            System.out.println(response.errorBody().string().toString());


                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });

SignBody class as below:

public class SignBody {

    @SerializedName("signin[username]") @Expose private String username;
    @SerializedName("signin[password]") @Expose private String password;

    public SignBody(String username, String password) {
        this.username = username;
        this.password = password;
    }

} 

finally Interfacem.class :

public interface Interfacem {
    @POST("/login")
    Call<ResponseBody> getResponse(@Body SignBody body);

}

There is not any problem when I tried postman same think. postman screenshot

Upvotes: 1

Views: 1058

Answers (3)

Kaushal Patel
Kaushal Patel

Reputation: 338

Which version you use in retrofit? Below code for version 2.0. If any Header require please check. If you can share url and username or password. I will check.

public interface Interfacem {

    @FormUrlEncoded
    @POST("login")
    Call<ResponseBody> getResponse(@Field("username") String username,@Field("password")String password );
}


Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .build();

                Interfacem service = retrofit.create(Interfacem.class);
                Call<ResponseBody> result =service.getResponse("myUsername","myPassword");
                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            System.out.println(response.body().string().toString());
                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });

Upvotes: 3

ThaiPD
ThaiPD

Reputation: 3761

Change the interface class as (remove the slash in the path)

    public interface Interfacem {
      @POST("login")
      Call<ResponseBody> getResponse(@Body SignBody body);
    }

You can refer the reason in my answer here

Upvotes: 1

gilgil28
gilgil28

Reputation: 540

It seems like there are no getters in your body class.

Upvotes: 0

Related Questions