在使用Retrofit 2.1.0的GET方法时,出现了java.lang.IllegalArgumentException: baseUrl必须以/结尾的错误。

18

我正在使用 Retrofit2 进行 API 解析。

在使用 retrofit1.9.0 时,无论是 post 方法还是 get 方法都能正常工作。但使用 retrofit 2.1.0 时,对于 get 方法,会出现以下错误:

java.lang.IllegalArgumentException: baseUrl must end in /

我已经检查了我的代码,问题不大,post 方法也可以正常运行。

    Retrofit retrofit= new Retrofit.Builder() 
                        .baseUrl("sample.com/ecomtest/index.php?route=api/") 
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

请添加相关的代码。 - J Fabian Meier
请记得分享一些相关的代码,这可以帮助人们快速解决您的问题。 - Manish
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://sample.com/ecomtest/index.php?route=api/") .addConverterFactory(GsonConverterFactory.create()) .build(); - Prasanth
这段代码出现了异常,导致应用程序崩溃。 - Prasanth
1
“sample.com/ecomtest/”作为基本url。 - eriuzo
我已经尝试过了,我的 API URL 是 "sample.com/ecomtest/index.php?route=api/login&email={email}&password={password}"。我正在使用 @Path 来传递参数电子邮件和密码字段。但是在检查时,“?route=api” 存在问题,是否有任何解决错误的方法? - Prasanth
7个回答

40

“?”不能在baseUrl中,您应该将它移动到API接口中。就像这样:

完整的URL:https://free-api.heweather.com/v5/now?city=yourcity&key=yourkey

所以,baseUrl = "https://free-api.heweather.com/v5/"

而API接口为:

@GET("now?")
Observable<HeWeather5> getNowWeather(@Query("city") String city,@Query("key") String key);

很好的解释,帮助了我。 - Milos
1
为什么 @GET("now?") 中有 "?",既然我们已经提供了 @Query("city"),应该不再需要了。 - Mihae Kheel

16

您传递的API_BASE_URL应以"/"结尾,因此请在URL末尾添加它。

Retrofit.Builder builder =
         new Retrofit.Builder()
                     .baseUrl(API_BASE_URL)
                     .addConverterFactory(GsonConverterFactory.create());

何处

String API_BASE_URL = "http://www.domain.com/"; //string end with "/"

它会起作用。


我也检查了这种情况,但问题仍然存在。 - Prasanth
1
你在URL中的“?”后面添加“/”,这意味着它作为参数传递。 例如:sample.com/ecomtest/,并将路由传递给参数。 - RBK
谢谢RBK,但是在使用它在retrofit1时仍然出现问题,对于相同的base_url="sample.com/ecomtest/index.php?route=api/"它可以完美地工作。 - Prasanth
我遇到了同样的问题。我的基本URL是“sample.com/index.php/”,API端点是“?route=api/getList”....当我运行应用程序时,我得到的URL是“sample.com/index.php/?route=api/getList”,而不是“sample.com/index.php?route=api/getList”... :( - Hitesh Dhamshaniya

6
在 Retrofit 中,您不能在基本 URL 中使用“some?”等端点。请将您的 URL 拆分为:
String url="http://sample.com/ecomtest/index.php?route=api/";
String baseUrl=url.split(".com/")[0]+".com/";
String queryUrl=url.split(".com")[1];

那么使用


Retrofit.Builder builder =
         new Retrofit.Builder()
                     .baseUrl(baseUrl)
                     .addConverterFactory(GsonConverterFactory.create());

并将您的终点作为路径传递给请求方法,例如:
@GET("{endpoint}")
    Call<Response> getData(@Path("endpoint") String endpoint);

并完成


5
假设你的URL是 "https://yourapi.com/abc?def&key=123",请将其分成两个部分。
String baseURL ="https://yourapi.com/"; //make sure you have '/' at the end

String key = "123";

然后在GET注释中添加其他内容,就像这样 @GET("abc?def&key="+key)

注意:请勿修改HTML标签。

1
你是天才,我向您致敬。 - Tonnie

3

我遇到过同样的问题,这里的答案几乎覆盖/解释了解决方案。

然而并不完全正确。

我尝试这样存储BASE_URL: sample.com/api 我的端点将是例如:/users

添加/的解决方案没有帮助:sample.com/api/

问题在于BASE_URL中有多个/,我猜测retrofit对此感到困惑,因为斜杠不仅仅是结尾标记。

唯一有效的解决方案是:sample.com/ 相应地,端点为:api/users 因此,对我而言,保留BASE_URL中只有一个/,并将其保留在基础URL字符串的末尾是解决方案。


0

一定要设置 [encoded = false],像这样:

suspend fun getAnyThing(@Query("api_key", encoded = false) apikey: String)

-4

我通过使用URL = "URL/"解决了这个错误。必须将/作为最后一个字符才能使其正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接