改造,湿地的路径中的字符 [英] Retrofit and Slash characters in PATH

查看:201
本文介绍了改造,湿地的路径中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着改造的问题,想找到一个合适的答案,因为我认为它的唯一途径是pretty丑陋和不实用。

I am facing an issue with Retrofit and would like to find a suitable answer as the only way I can think of it is pretty ugly and not practical.

改型路径注释需要一个/在开始的时候(你可以从库源中提取这code阅读:

Retrofit PATH annotation requires a "/" in the beginning (as you can read in this code extracted from the library source:

/** Loads {@link #requestUrl}, {@link #requestUrlParamNames}, and {@link #requestQuery}. */
  private void parsePath(String path) {
    if (path == null || path.length() == 0 || path.charAt(0) != '/') {
      throw methodError("URL path \"%s\" must start with '/'.", path);
    }

这是我现在面临的问题是,路径部分来自于一个响应对象后端,这意味着所有的路径的字符串已经从后端$ P $其他反应过来格式化pviously如下:

The problem that I am facing is that the PATH part comes from the backend in a response object, meaning that all PATH's strings already come formatted from the backend previously in other response as follows:

Object : {
    href: "/resources/login..."
}

正如你所看到的,包括这样的事情时,该URL被畸形的:

As you can see, when including something like this, the URL gets malformed:

@GET("{/loginHref}")
    void login(@EncodedPath("loginHref") String loginHref,
               Callback<User> callback);

要像前面的 http://mybaseurl.com//resources/login *双//资源

to something like "http://mybaseurl.com//resources/login" *double // in front of resources

这肯定会产生一些端点的问题,我想不出一个非常简单的方法,从做这样的事情除了解决此问题:

This can definitely cause issues in some endpoints and I cannot think a really simple way to solve this issue apart from doing something like:

一)修改我自己改装的版本删除/字符检查(这是不得已而为之)

a) Modify my own version of retrofit to remove that / character check (this is a last resort)

b)使用该接口中的方法(我想不惜一切代价避免,以及会增加不必要的改造所有的地方之​​前截断HREF。

b) Truncate the href before using the method from the interface (which I would like to avoid at all cost as well as would add unnecessary transformation all over the place.

C)拦截请求,并形成正确的情况下,这种情况发生的(真正丑陋的解决方案以及)的URL。

c) Intercept the request and correctly form the URL in case this scenario happens (really ugly solution as well).

任何想法,建议?

谢谢!

推荐答案

我认为这个链接将帮助您路径置换

I think this link will help you Path Replacement

您新的实现看起来是这样的。

Your new implementation will look like this.

@GET("/")
void login(Callback<User> callback);

您可以提供自定义端点实现上,你可以改变的相对路径。

You can supply a custom Endpoint implementation on which you can change the relative path.

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

  private String url;
  private String href;      

  public CustomEndpoint(String href){
      this.href = href;
      url = BASE + this.href;
  }

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

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

用法如下:

RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(new CustomEndPoint(object.href));
then restadapter.create........

希望这会帮助你。

Hope this will help you.

这篇关于改造,湿地的路径中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆