Retrofit2 报错baseUrl must end in /

依赖

// Rxjava

implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

implementation 'io.reactivex.rxjava2:rxjava:2.0.5'

// retrofit 网络 请求

implementation 'com.squareup.retrofit2:retrofit:2.4.0'

// implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

implementation ('com.squareup.retrofit2:converter-gson:2.7.0'){

exclude group : 'com.google.code.gson'

}

现象:

builder.baseUrl(http://xxx.xxx.com/cric);

​或

builder.baseUrl(http://xxx.xxx.com/cric/);

@POST("/index.php?m=&c=app&a=sendSMS")

Call sendSMS2(@Body RequestBody body);

请求时报错现象可能又两种

错误1. baseUrl must end in /: http://xxx.xxx.com/cric

错误2:打印日志显示 的请求路径:http://xxx.xxx.com/index.php?m=&c=app&a=sendSMS.重路径中看到少了 /cric 这个。

解答案决:

// 设置路径最后面带斜杠 “/”

builder.baseUrl(http://xxx.xxx.com/cric/);

// 接口这里最前面不要带斜杠 “/”

@POST("index.php?m=&c=app&a=sendSMS")

Call sendSMS2(@Body RequestBody body);

详细原因参考:Retrofit2 的baseUrl 真的必须以 /(斜线) 结尾吗? - 简书

参考文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: