Retrofit2如何将/放在动态baseUrl的末尾?

时间:2016-04-20 07:34:30

标签: android retrofit2

我已经在像这样的片段中使用Parcelable从json响应发送并检索了一个url String

收到字符串值

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

2 个答案:

答案 0 :(得分:2)

由于你有一个完整的网址,你需要在Retrofit2中使用@Url注释。

使用类似的东西定义界面

task_task_path

然后用

调用它
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);
       }
    }
}

答案 1 :(得分:0)

你的字符串变量的末尾必须有“/”。

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