I'm just trying to consume services with Android Studio and GET services have worked well, but with POST services it does not work for me. I have tried several ways that I have found out there and the answer always goes by the onFailure side.
In my interface I have the following:
public interface APIService {
@FormUrlEncoded
@POST("nombreServicio")
@Headers("Content-Type: application/x-www-form-urlencoded")
Call<RespuestaPost> postRegistrar(
@Field("param1") String param1,
@Field("param2") String param2,
@Field("param3") String param3); }
The ResponsePost class with their respective set and get:
public class RespuestaPost {
private String mensaje, respuesta, data;
private int estado;
//AQUÍ TODOS LOS MÉTODOS SET Y GET
}
The retrofit client that has worked for GET services:
public class RetrofitClient {
public static final String BASE_URL = "https://192.168.1.xx:xxxx/serviciosExternos/api/v1/app/";
public static APIService RETROFIT_CLIENT;
public static APIService getInstance() {
//if REST_CLIENT is null then set-up again.
if (RETROFIT_CLIENT == null) {
setupRestClient();
}
return RETROFIT_CLIENT;
}
private static void setupRestClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RETROFIT_CLIENT = retrofit.create(APIService.class);
}
}
And finally in my MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "CONSOLA: ";
TextInputEditText etParam1, etParam2, etParam3;
Button btn1;
private APIService mAPIService;
Call<RespuestaPost> call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAPIService = RetrofitClient.getInstance();
etParam1 = findViewById(R.id.Param1);
etParam2 = findViewById(R.id.Param2);
etParam3 = findViewById(R.id.Param3);
btn1 = findViewById(R.id.Btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String p1= etParam1.getText().toString().trim();
String p2= etParam2.getText().toString().trim();
String p3= etParam3.getText().toString().trim();
if(!TextUtils.isEmpty(p1) && !TextUtils.isEmpty(p2) && !TextUtils.isEmpty(p3)) {
sendPost(p1, p2, p3);
}else{
//INCOMPLETO
}
}
});
}
private void sendPost(String p1, String p2, String p3) {
call = mAPIService.postRegistrar(p1, p2, p3);
call.enqueue(new Callback<RespuestaPost>() {
@Override
public void onResponse(Call<RespuestaPost> call, Response<RespuestaPost> response) {
Log.i(TAG, "Se envió exitoso.");
if (response.isSuccessful()) {
// tasks available
String respuesta = response.body().toString();
Log.i(TAG, "Respuesta: " + respuesta);
} else {
// error response, no access to resource?
Log.i(TAG, "1. Error response");
}
}
@Override
public void onFailure(Call<RespuestaPost> call, Throwable t) {
Log.e(TAG, "2. Error no se envió.");
}
});
}
}
With GET services it gives me the normal answer, but with the POST services it always throws the onFailure response: "2. Error was not sent.". I appreciate if someone could tell me if I'm doing something wrong in the process, since the services work correctly when consumed from other places like Postman.
Edit:
After putting in Log.e(TAG, "2. Error no se envió. "+t.getMessage());
I got the following error:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Edit: The error was because of the SSL certificate. In my case the solution given by Retrofit can be found in the following link: Retrofit 2 - How to trust unsafe SSL certificates (self-signed , expired)