1

Currently I am facing some issues with Retrofit. The URL, I am providing to RetrofitInstance is changing for the second request. Here are the codes:

object RetrofitClientInstance{

     private var retrofit: Retrofit? = null
     //private const val BASE_URL = "http://api.sample.com/req/";
     private const val BASE_URL = "http://test.sample.com/req/"
     private const val BASE_URL_VERSION = "v1/"


     fun getRetrofitInstance() : Retrofit {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL + BASE_URL_VERSION)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
        }
        return this!!.retrofit!!
    }

}

Here are the interface methods for different API requests:

class UserLoginResponseReceiver{

    interface GetDataService {

        @FormUrlEncoded
        @POST(UrlEndPoints.USER_LOGIN_BY_FACEBOOK)
        fun loginUserByFacebook(@Field("access_token") last: String): Call<FbLoginResponse>

        @GET(UrlEndPoints.ALL_POSTS)
        fun getAllPosts() : Call<AllPostsResponse>

    }
}

UrlEndPoints.kt

object UrlEndPoints {

   const val USER_LOGIN_BY_FACEBOOK = "user/auth/facebook"
   const val ALL_POSTS = "post"
}

For the first request (loginUserByFacebook), the URL I am getting by debugging my response is:

http://test.sample.com/req/v1/user/auth/facebook

Which is fine and working perfectly. But for the second request (getAllPosts()) I am getting the following URL:

http://test.sample.com/post

The "req/v1" part is completely cut off! As a result I don't get the desired response. What's the problem here actually? Please help me.

noob-Sci-Bot
  • 231
  • 5
  • 18
  • 1
    Possible duplicate of [Retrofit 2 removes characters after hostname from base url](https://stackoverflow.com/questions/32352159/retrofit-2-removes-characters-after-hostname-from-base-url) – michalbrz Jun 20 '18 at 10:19
  • Not a duplicate of that question - that would be the expected behavior only if the annotation was declared with a leading slash, like `@GET("/post")`. Using `@GET("post")` should keep the entire base url, not just the host. Are you sure that your `ALL_POSTS` constant doesn't have a slash in it, @noob-Sci-Bot? – zsmb13 Jun 20 '18 at 10:22
  • @zsmb13 Yes, I am absolutely sure. – noob-Sci-Bot Jun 20 '18 at 10:24
  • 1
    Which version of Retrofit are you using? – peshkira Jun 20 '18 at 11:38
  • `com.squareup.retrofit2:retrofit:2.3.0` – noob-Sci-Bot Jun 20 '18 at 11:43

1 Answers1

2

I can't reproduce this, you should try cleaning / rebuilding your project, the code you've provided here has no errors in it.

This is the code I added to test it:

class AllPostsResponse
class FbLoginResponse

fun main(args: Array<String>) {
    val service = RetrofitClientInstance.getRetrofitInstance()
                     .create(UserLoginResponseReceiver.GetDataService::class.java)
    val url = service.getAllPosts().request().url()
    println(url)
}

The URL returned here is the following, as expected:

http://test.sample.com/req/v1/post
zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • Thanks, for the reply. That's the desired response. But for some reason, I am not getting that. – noob-Sci-Bot Jun 20 '18 at 10:47
  • I've another question. May be a naive one. If the response is not according to the predefined model, How would the error look? – noob-Sci-Bot Jun 20 '18 at 10:49
  • 1
    You'd be getting an error from inside `Gson`, something like a `com.google.gson.JsonSyntaxException`. – zsmb13 Jun 20 '18 at 10:50
  • Thank you. But I don't understand the problem above, stuck here for almost a week now. Can you please replicate the exact scenario if possible? – noob-Sci-Bot Jun 20 '18 at 11:17
  • I had this exact problem. I had leading slashes in my endpoints, and they were messing up the url. However, when I removed the leading slashes, the problem still persisted. What fixed it was rebuilding the whole project from scratch. I completely removed my app from the phone. Then, using `./gradlew clean` I removed all build cache. After that, I built the project, and everything was working. To summarize: you have to remove the leading slashes, and do a complete rebuild of your project. – acmpo6ou Oct 04 '22 at 12:36