【问题标题】:java.lang.IllegalArgumentException: baseUrl must end in / while using retrofit 2.1.0 for GET methodjava.lang.IllegalArgumentException:baseUrl 必须以 / 结尾,而对 GET 方法使用改造 2.1.0
【发布时间】:2021-07-13 03:17:12
【问题描述】:

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

在使用retrofit1.9.0 时,post 和 get 方法都可以正常工作。但是使用retrofit 2.1.0,在get方法中,出现错误:

java.lang.IllegalArgumentException: baseUrl 必须以 / 结尾

我检查了我的代码,没有问题,它适用于 post 方法。

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

【问题讨论】:

  • 请添加相关代码。
  • 请记得分享一些相关代码,帮助人们快速为您解决问题。
  • Retrofit retrofit= new Retrofit.Builder() .baseUrl("sample.com/ecomtest/index.php?route=api/") .addConverterFactory(GsonConverterFactory.create()) .build();
  • 在此代码中出现异常并且应用程序崩溃
  • 使用"sample.com/ecomtest/"作为基础网址

标签: java android


【解决方案1】:

'?'不能在 baseUrl 中,您应该将其移至 API 接口。像这样。

完整网址: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);

【讨论】:

  • 很好的解释,帮助了我。
  • 为什么@GET("now?") 有“?”据说不再需要,因为我们已经提供了@Query("city")
【解决方案2】:

您作为API_BASE_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 "/"

它会起作用的。

【讨论】:

  • 我也检查过这种情况,但问题仍然存在
  • 您在“?”之后添加“/”在 URL 中,这意味着它作为参数。 "sample.com/ecomtest/" 并在参数中传递路由。
  • 感谢 RBK,但在改造中使用它时仍然会出现问题1 它适用于相同的 base_url="sample.com/ecomtest/index.php?route=api/"
  • 我面临同样的问题。我的基本 URL = "sample.com/index.php/" API 端点是 "?route=api/getList" .... 运行应用程序时,我得到了类似 "sample.com/index.php/?route= api/getList" 而不是 "sample.com/index.php?route=api/getList"... :(
【解决方案3】:

在改造中你不能使用像“some?”这样的端点。在基本 URL 中。 像

一样拆分你的 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);

完成了

【讨论】:

    【解决方案4】:

    假设您的网址是“https://yourapi.com/abc?def&key=123

    将您的网址分成两部分。

    String baseURL ="https://yourapi.com/"; //make sure you have '/' at the end
    
    String key = "123";
    

    然后像这样@GET("abc?def&amp;key="+key)在GET注解中添加rest

    【讨论】:

    • 你是天才,我向你的先生致敬
    【解决方案5】:

    这里的情况和答案几乎涵盖/解释了解决方案。 然而不完全是。

    我试图像这样存储BASE_URLsample.com/api 我的端点将用于 e.x:/users

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

    问题在于BASE_URL 中有不止一个/,我猜改造会因为斜线不仅仅是结束标记而疯狂。

    唯一有效的解决方案:sample.com/ 终点是:api/users 因此,在 BASE_URL 中只保留一个 / 是我的解决方案,并将其保留在基本 url 字符串的末尾

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 2018-08-16
          • 2019-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-27
          • 2011-07-05
          • 2011-09-22
          相关资源
          最近更新 更多