F1s*_*her 17 java rest android retrofit
我的apiPath是完全动态的.我正在使用包含"ipAddress"和"SSLprotocol"等字段的项目.基于它们我可以建立我的网址:
private String urlBuilder(Server server) {
String protocol;
String address = "";
if (AppTools.isDeviceOnWifi(activity)) {
address = serverToConnect.getExternalIp();
} else if (AppTools.isDeviceOnGSM(activity)) {
address = serverToConnect.getInternalIp();
}
if (server.isShouldUseSSL()) {
protocol = "https://";
} else {
protocol = "http://";
}
return protocol + address;
}
Run Code Online (Sandbox Code Playgroud)
所以我的协议+地址可以是:http:// + 192.168.0.01:8010 = http://192.168.0.01:8010
我想这样使用它:
@FormUrlEncoded
@POST("{fullyGeneratedPath}/json/token.php")
Observable<AuthenticationResponse> authenticateUser(
@Path("fullyGeneratedPath") String fullyGeneratedPath,
@Field("login") String login,
@Field("psw") String password,
@Field("mobile") String mobile);
Run Code Online (Sandbox Code Playgroud)
因此,authenticateUser的完整路径将是http://192.168.0.01:8010/json/token.php - 例如.
这意味着我不需要任何basePath,因为我自己创建整个basePath,具体取决于我想要连接的服务器.
我的改造设置是:
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient,
Converter.Factory converterFactory,
AppConfig appConfig) {
Retrofit.Builder builder = new Retrofit.Builder();
builder.client(okHttpClient)
.baseUrl(appConfig.getApiBasePath())
.addConverterFactory(converterFactory)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
return builder.build();
}
Run Code Online (Sandbox Code Playgroud)
如果我删除baseUrl然后我得到错误,该参数是必需的.所以我将apiBasePath设置为:
public String getApiBasePath() {
return "";
}
Run Code Online (Sandbox Code Playgroud)
然后我在创建改造实例后立即收到错误:
java.lang.IllegalArgumentException: Illegal URL:
Run Code Online (Sandbox Code Playgroud)
怎么解决?
Inv*_*rce 34
从源(新URL解析概念),您只需在post请求中指定整个路径.
此外,我们还可以在Retrofit 2.0中的@Post中声明一个完整的URL:
Run Code Online (Sandbox Code Playgroud)public interface APIService { @POST("http://api.nuuneoi.com/special/user/list") Call<Users> loadSpecialUsers(); }对于这种情况,将忽略基本URL.
就这样使用
public interface UserService {
@GET
public Call<ResponseBody> profilePicture(@Url String url);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14178 次 |
| 最近记录: |