簡體   English   中英

Retrofit2如何將/放在動態baseUrl的末尾?

[英]Retrofit2 How do i put the / at the end of the dynamic baseUrl?

我已經在像這樣的片段中使用Parcelable從json響應中發送並檢索了URL字符串

收到的字符串值

String profile_url = student.getProfile();

我想使用此字符串值作為basUrl來使用Retrofit發出另一個請求,如下所示

Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(profile_url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

但是出現以下錯誤

java.lang.RuntimeException: An error occured while executing doInBackground()

            ......

Caused by: java.lang.IllegalArgumentException: baseUrl must end in /:

如何將/放在baseUrl的末尾? 像這樣直接放

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(profile_url/)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

不起作用,快遞表示期望。

任何幫助。 謝謝

調試配置文件網址profile_url=http://services.hanselandstudent.com/student.svc/1.1/162137/category/1120/json?banner=0&atid=468f8dc2-9a38-487e-92ae-a194e81809d9

由於您有完整的要調用的網址,因此Retrofit2中將需要@Url批注。

用類似的東西定義你的界面

public interface YourApiService {
    @GET
    Call<Profile> getProfile(@Url String url);

    class Factory {
        public static YourApiService createService() {
             Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl("http://www.what.com/")
                .build();
             return retrofit.create(YourApiService.class);
       }
    }
}

然后用

YourApiService.createService().getProfile(profile_url);

字符串變量的末尾必須有“ /”。

String profile_url = student.getProfile() + "/";
Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(profile_url)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

暫無
暫無

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

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