4
 Retrofit retrofit = new Retrofit.Builder()

            .baseUrl("http://ipAdress/SaveImg/DouBanGirl")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    imgApi imgService = retrofit.create(imgApi.class);
    Call<Img> imgCall = imgService.getImg("20151119");

    imgCall.enqueue(new Callback<Img>() {
        @Override
        public void onResponse(retrofit.Response<Img> response, Retrofit retrofit) {
            Log.d(TAG, response.code() + " ");
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d(TAG, t.getMessage());

        }
    });

}

public interface imgApi {
    @GET("/DouBanGirl")
    Call<Img> getImg(@Query("date") String date);
}

when i tried this, it is showing 404 not found. the url is correct, i checked that. I dont know what going on.

HugoXie
  • 43
  • 1
  • 3
  • Have you included the port in base url? – Héctor Dec 10 '15 at 15:51
  • If you get a 404 the URL does not exist at the server. Which URL do you want to connect to? – Henry Dec 10 '15 at 16:00
  • You have DouBanGirl both in base url and in GET path. I doubt, that it's correct :) Use baseUrl("http://ipAdress/SaveImg/DouBanGirl") and GET("/") or baseUrl("http://ipAdress/SaveImg") and @GET("/DouBanGirl") – Vasilov Artur Dec 10 '15 at 16:00

2 Answers2

12

Due to how Retrofit 2.0 uses Http Resolve for resolving the uri scheme of your endpoints, if you specify the baseurl like this http://hello.com and the endpoint URL as /world/foo it will break.

You need to use base URL http://hello.com/ and endpoint URL world/foo.

The / makes the difference.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I have edited it as baseUrl `http://ipAdress/SaveImg/DouBanGirl/ ` And `@GET("DouBanGirl")`, BUT it doesn't work , 404 again – HugoXie Dec 11 '15 at 08:06
  • @HugoXie Leave the base URL as `.baseUrl("http://ipAdress/")` and path at interface as `@GET("DouBanGirl")` – kgori_dev Jan 11 '21 at 08:49
0

The question were asked a while ago, but I answer this just in case anyone reached here in the future. The Retrofit does not recognize 200 status code the way web browser or Postman does. It only detects 200 if only you send it bundled with an empty json, otherwise it acts like the server gave it 404 status code. For example in Express js the code res.sendStatus(200); is not making Retrofit considers the task done and you should use something like res.status(200).send({});

asajadi84
  • 410
  • 5
  • 20