kanudo
kanudo

Reputation: 2219

Retrofit does not deliver Java Object as POST parameters on PHP backend from Android

Using Retrofit I want to POST values of Java Object as parameters to the PHP script on backend side. But using the code below i am not able to receive any parameters inside $_POST variable.

Here is how I am doing this right now.

1) This is how I build and get the Retrofit Instance

public class RetrofitClient {

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://www.example.com";

    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

2) The Model of the parameters being sent

public class LoginModel {

    @SerializedName("email")
    private String email;

    @SerializedName("password")
    private String password;

    public LoginModel(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public String getEmail() { return email; }
    public String getPassword() { return password; }
}

3) The API Interface

public interface RequestAPI {

    @POST("/login")
    Call<ResponseBody> getResponse(@Body LoginModel loginModel) ;
}

4) And this is how I fire the request and catch the response

@Override
public void onClick(View v) {

    LoginModel loginModel = new LoginModel("[email protected]", "123");

    RequestAPI service = RetrofitClient.getInstance().create(RequestAPI.class);

    Call<ResponseBody> call = service.getResponse(loginModel);

    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.e("=====", response.body().string());
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("=====", "error", t);
        }
    });
}

5) PHP script in backend

echo json_encode($_POST);

Now every time i dump the $_POST variable. It just gives an empty array.

Am I missing on any thing.

Why am i not receiving the email and password as the child of $_POST?

Upvotes: 1

Views: 986

Answers (1)

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

Use like this for API method:

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

& call like this:

@Override
public void onClick(View v) {

LoginModel loginModel = new LoginModel("[email protected]", "123");

RequestAPI service = RetrofitClient.getInstance().create(RequestAPI.class);

Call<ResponseBody> call = service.getResponse(loginModel.getEmail(), loginModel.getPassword());

call.enqueue(new Callback<ResponseBody>() {

    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        try {
            Log.e("=====", response.body().string());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("=====", "error", t);
    }
});
}

Edit:

If you still want to pass custom object as raw body of API call then there will be change for PHP side code :

Instead of using

echo json_encode($_POST);

consider using

$post_body = file_get_contents('php://input');

as this alternate method provides whole raw request body as variable directly rather than providing it as JSON object to be encode.

Upvotes: 5

Related Questions