1

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")

3 Answers3

0

Posible solutions

1: if your service is local,remember use

Genymotion

http://10.0.3.2/

Default AVD

http://10.0.2.2/

2: Check if your endpoints are corrects

usersFake
login

3:In my case in my local i have nginx and i have a problem accessing using emulator

Community
  • 1
  • 1
David Hackro
  • 3,652
  • 6
  • 41
  • 61
  • My service is not local is posted on Internet, my endpoints are correct actually I reach my WS but my response is null on retrofit callback (on server log I see the whole process with a valid response) :( – Oscar Reynaldo Flores Jiménez Feb 09 '17 at 22:55
  • maybe your json responde no it's correct, please verify using this pages http://www.jsonschema2pojo.org/ ---- http://jsonlint.com/ – David Hackro Feb 10 '17 at 00:08
  • Is the same service that I actually use, I already have an app developed with all this classes and services but now I want to move it to clean architecture I used to do my calls to my client with an http client and then parse it with gson I'm using the same, it can't be the object or the json (the json is on the post) that why I don't understand what's going on :( – Oscar Reynaldo Flores Jiménez Feb 10 '17 at 01:51
0

I faced the same issue yesterday. Try this:

Check your working URL in browser. If your url endpoint has any extensions like .php, then add it in your GET/POST annotation parameter and write the fullname like @GET("userFake.php").

This worked for me. Hoping it also works for you.

sud007
  • 5,824
  • 4
  • 56
  • 63
Siddhpura Amit
  • 67
  • 2
  • 10
0

While change in BASE URl ,it works for me.Change your BaseUrl Like this:

  private static final String BASE_URL="http://abcd.com/abcd/index.php/";

And Make endpoints like this

@GET("api/Abcd_controller/users")
    Call<ABCDList>getUsers();

For more details see this link. This work for me .Hope this will also work for you. Thanks

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30