retrofit 2 404 not found

巧了我就是萌 提交于 2019-12-24 06:52:05

问题


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


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/42145438/retrofit-2-404-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!