簡體   English   中英

java.lang.IllegalArgumentException:baseUrl必須以/在使用改進2.1.0 for GET方法時結束

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

我正在使用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();

'?' 不能在baseUrl中,你應該將它移動到API接口。 像這樣。

完整的網址: https://free-api.heweather.com/v5/now? city=yourcity&key=yourkeyhttps://free-api.heweather.com/v5/now? city=yourcity&key=yourkey 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);

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

它會工作。

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

並做了

假設您的網址是“ 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)

我使用URL = "URL/"解決了這個錯誤。 必須有/作為使它工作的最后一個角色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM