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("user@example.com", "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?