Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改造中的动态路径_Android_Rest_Httprequest_Retrofit - Fatal编程技术网

Android 改造中的动态路径

Android 改造中的动态路径,android,rest,httprequest,retrofit,Android,Rest,Httprequest,Retrofit,我正在尝试使用likehttp://192.168.1.64:5050/api/{api_key}/updater.info 如何动态设置api\u键参数?我尝试使用RequestInterceptor,但在基本url为http://192.168.1.64:5050/api/{api_key} @Override public void intercept(RequestFacade request) { request.addPathParam("api_key", apiKey);

我正在尝试使用like
http://192.168.1.64:5050/api/{api_key}/updater.info

如何动态设置
api\u键
参数?我尝试使用
RequestInterceptor
,但在基本url为
http://192.168.1.64:5050/api/{api_key}

@Override
public void intercept(RequestFacade request) {
    request.addPathParam("api_key", apiKey);
}

还有其他选择吗?

路径替换不会发生在API端点的基本URL内,只会发生在方法上的相对URL字符串内。我假设您不想在每个接口方法声明上都添加相对URL的前缀

虽然措辞拙劣,但javadoc声明:

调用者应该总是向实例查询最新的值,而不是缓存返回的值

这意味着对于每个请求,
端点
实例将被查询基本URL的值

您可以提供自定义的
端点
实现,您可以在其上更改API键值:

public final class FooEndpoint implements Endpoint {
  private static final String BASE = "http://192.168.1.64:5050/api/";

  private String url;

  public void setApiKey(String apiKey) {
    url = BASE + apiKey;
  }

  @Override public String getName() {
    return "default";
  }

  @Override public String getUrl() {
    if (url == null) throw new IllegalStateException("API key not set.");
    return url;
  }
}

例如,如果路径参数不在每个请求的url中的相同位置,
http://endpoint/blah/{apiKey}
http://endpoint/blah/blah/{apiKey}/blah
,您可以执行以下操作

在您的API服务接口中

    @GET(/blah/{apiKey})
    void getFoo(Callback<Object> callback);

    @GET(/blah/blah/{apiKey}/blah)
    void getFooBlah(Callback<Object> callback);
使用以下命令:

@PUT("/path1/path2/{userId}")
void getSomething(
        @Path("userId") String userId
);
您可以这样调用该方法:

String userId = "1234";
service.getSomething(userId);

你介意看一看我的尝试吗?
String userId = "1234";
service.getSomething(userId);