问题
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