java baseurl_java.lang.IllegalArgumentException: baseUrl must end in / while using retrofit 2.1.0 fo...

问题

I am using Retrofit2 for API parsing.

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

java.lang.IllegalArgumentException: baseUrl must end in /

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

Retrofit retrofit= new Retrofit.Builder()

.baseUrl("sample.com/ecomtest/index.php?route=api/")

.addConverterFactory(GsonConverterFactory.create())

.build();

回答1:

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

The full url :https://free-api.heweather.com/v5/now?

city=yourcity&key=yourkey

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

and the API interface

@GET("now?")

Observable getNowWeather(@Query("city") String city,@Query("key") String key);

回答2:

The URL you have passed as API_BASE_URL should end with "/" so add it at the end of your 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.

回答3:

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

Split you URL like

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 getData(@Path("endpoint") String endpoint);

and done

回答4:

Suppose your url is "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)

回答5:

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

However not exactly.

I tried to store BASE_URL like this: sample.com/api

And my endpoints would be for e.x: /users

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

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

The only solution that worked: sample.com/

And the endpoint for that would be: api/users

So keeping just one / in the BASE_URL was the solution for me, and keep it at the end of the base url string

回答6:

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

来源:https://stackoverflow.com/questions/38395460/java-lang-illegalargumentexception-baseurl-must-end-in-while-using-retrofit-2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值