I'm trying to use retrofit but i get 404 code and message "not found"
Response {protocol=http/1.1, code=404, message=Not Found}
i can see on my server log that the request was successfully and the response is and object
I used to parsed it with Gson
{
"codigoRespuesta": "00",
"descRespuesta": "Success",
"token": "0bc6e8a352a002db5782ae729501fd05bd825cf165864a0ab32d1ab1529001ed2583df4a5ab257a30f97117ec0fec0b6df60adf288fd6b1579f31d2d636d9aa"
}
so I'm using the same object with retrofit that i used to use with asyncs request
public class AuthenticationResponse extends Response {
private String token;
public AuthenticationResponse() {
}
public AuthenticationResponse(String codigoRespuesta, String descRespuesta) {
super(codigoRespuesta, descRespuesta);
}
public AuthenticationResponse(String codigoRespuesta, String descRespuesta, String token) {
super(codigoRespuesta, descRespuesta);
this.token = token;
}
public String getToken() {
return this.token;
}
public void setToken(String token) {
this.token = token;
}
public String toString() {
return "AuthenticationResponse{token=" + this.token + ", " + super.toString() + '}';
}
}
@XmlRootElement
public class Response {
private String codigoRespuesta;
private String descRespuesta;
public Response() {
}
public Response(String codigoRespuesta, String descRespuesta) {
this.codigoRespuesta = codigoRespuesta;
this.descRespuesta = descRespuesta;
}
@XmlElement
public String getCodigoRespuesta() {
return this.codigoRespuesta;
}
public void setCodigoRespuesta(String codigoRespuesta) {
this.codigoRespuesta = codigoRespuesta;
}
@XmlElement
public String getDescRespuesta() {
return this.descRespuesta;
}
public void setDescRespuesta(String descRespuesta) {
this.descRespuesta = descRespuesta;
}
public String toString() {
return "codigoRespuesta=" + this.codigoRespuesta + ", descRespuesta=" + this.descRespuesta;
}
}
This projects is based on this example github project
My retrofit interface
public interface servicesTutorial {
@GET("usersFake")
Call<List<ResponseService>> getUsersGet();
@POST("usersFake")
Call<List<ResponseService>> getUsersPost();
@POST("login")
Call<AuthenticationResponse> doLogin(@Body RequestLogin request);
}
My MainActivity class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new GsonBuilder()
.setLenient()
.create();
final String url = "https://myurl.com/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
servicesTutorial serviceTuto = retrofit.create(servicesTutorial.class);
Call<AuthenticationResponse> call2 = serviceTuto.doLogin(new RequestLogin("user","pwd"));
call2.enqueue(new Callback<AuthenticationResponse>() {
@Override
public void onResponse(Call<AuthenticationResponse> call, Response<AuthenticationResponse> response) {
Log.e("testing", "respuesta: "+response.message()+" *** "+response.code()+" *** "+response.isSuccessful() +
" *** " + response.raw().toString());
}
@Override
public void onFailure(Call<AuthenticationResponse> call, Throwable t) {
Log.e("testing",t.toString());
}
});
}
}
EDIT: I finally got it, appareantly because my services was made with Java and Spring I had to add a header to my request ("Accept: application/json")