java.lang.IllegalArgumentException:baseUrl 必须以 / 结尾,同时对 GET 方法使用改造 2.1.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38395460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:16:42  来源:igfitidea点击:

java.lang.IllegalArgumentException: baseUrl must end in / while using retrofit 2.1.0 for GET method

javaandroid

提问by Prasanth

I am using Retrofit2 for API parsing.

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

While using retrofit1.9.0both post and get methods work properly. But using retrofit 2.1.0, in the get method, there is an error:

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

java.lang.IllegalArgumentException: baseUrl must end in /

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

I have checked my code and there is no problem, it's working for the post method.

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

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

回答by RBK

The URL you have passed as API_BASE_URLshould end with "/" so add it at the end of your URL.

您传递的 URLAPI_BASE_URL应以“/”结尾,因此请将其添加到 URL 的末尾。

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

where

在哪里

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

it will work.

它会起作用。

回答by u8337422

'?' can not in baseUrl, you should move it to API interface. like this.

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

The full url :https://free-api.heweather.com/v5/now? city=yourcity&key=yourkey

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

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

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

and the API interface

和 API 接口

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

回答by saykopat

I solved this Error by using URL = "URL/". There has to be / as the last character to make it work.

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

回答by Mirza Ahmed Baig

In retrofit you can not use endpoints like "some?" in base URL. Split you URL like

在改造中你不能使用像“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];

then use

然后使用

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

and pass you end point as a path to request method like

并将您的端点作为请求方法的路径传递,例如

@GET("{endpoint}")
    Call<Response> getData(@Path("endpoint") String endpoint);

and done

并做了

回答by Pemba Tamang

Suppose your url is "https://yourapi.com/abc?def&key=123"

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

Split your url into two parts.

将您的网址分成两部分。

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

String key = "123";

then add rest in the GET annotation like this @GET("abc?def&key="+key)

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

回答by M. Wojcik

Had the same situation and answers here almost cover/explain the solution. However not exactly.

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

I tried to store BASE_URLlike this: sample.com/apiAnd my endpoints would be for e.x: /users

我试图这样存储BASE_URLsample.com/api而我的端点将是例如:/users

The solution with adding /didn't help: sample.com/api/

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

The problem is with more than one /in the BASE_URLand I guess retrofit gets crazy about that the slash is not only as the ending mark.

问题是与一个以上/BASE_URL,我想改造得疯狂的斜线不仅作为结束标志。

The only solution that worked: sample.com/And the endpoint for that would be: api/usersSo keeping just one /in the BASE_URL was the solution for me, and keep it at the end of the base url string

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