Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装:Url正在更改_Android_Kotlin_Retrofit2 - Fatal编程技术网

Android 改装:Url正在更改

Android 改装:Url正在更改,android,kotlin,retrofit2,Android,Kotlin,Retrofit2,目前,我在改装方面面临一些问题我提供给的URL实例正在为第二个请求更改。以下是代码: 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/"

目前,我在改装方面面临一些问题我提供给
的URL实例
正在为第二个请求更改。
以下是代码:

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!!
    }

}
以下是针对不同API请求的
接口
方法:

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>

    }
}
对于第一个请求(loginUserByFacebook),我通过调试响应得到的URL是:

这是很好的和完美的工作。但是对于第二个请求(getAllPosts()),我得到了以下URL:


“req/v1”部分完全切断!结果我没有得到想要的回应。这里到底有什么问题?请帮助我。

我无法复制此内容,您应该尝试清理/重建您的项目,您提供的代码中没有错误

这是我为测试它而添加的代码:

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

可能重复的问题不是该问题的重复-只有在注释用前导斜杠声明时,这才是预期的行为,如
@GET(“/post”)
。使用
@GET(“post”)
应该保留整个基本url,而不仅仅是主机。你确定你的
所有帖子
常量中没有斜杠吗,@noob Sci Bot?@zsmb13是的,我绝对确定。你使用的是哪个版本的改型?
com.squareup.改型2:改型:2.3.0
谢谢你的回复。这是我们想要的回答。但出于某种原因,我不明白。我还有一个问题。可能是幼稚的。如果响应不符合预定义的模型,错误会是什么样子?您将从
Gson
内部得到一个错误,类似于
com.google.Gson.JsonSyntaxException
。谢谢。但我不明白上面的问题,在这里呆了将近一个星期。如果可能的话,你能复制一下确切的场景吗?
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)
}
http://test.sample.com/req/v1/post