gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:55:53 24 4
gpt4 key购买 nike

我已经在像这样的 fragment 中使用 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();

不工作, express 预期。

任何帮助。谢谢

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

最佳答案

既然你有一个完整的url来调用,你将需要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);

关于android - Retrofit2 如何将/放在动态 baseUrl 的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36736854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com