【问题标题】:Wrong Url in Retrofit 2.0Retrofit 2.0 中的错误 URL
【发布时间】:2016-01-14 12:09:15
【问题描述】:

我有一个使用 Eclipse、Maven 和 Retrofit 1.3 制作的项目。我想迁移到 Android Studio,我不得不在 gradle Retrofit 2.0 中导入。

在我进行了所有必要的更改以使其正常工作后,我得到了一些意想不到的东西。

我的改造构建器设置如下:

Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://localhost:8080/openbravo/org.openbravo.service.json.jsonrest")
                .client(httpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson));

所以我的服务方法改为使用<Call<List<T>> 而不仅仅是List<T>

当我执行 GET 方法登录时,我收到此响应错误:

Response{protocol=http/1.1, code=404, message=Not Found, url=http://localhost:8080/ADUser?_where=username%3D%27Openbravo%27}

但网址应该是:

http://localhost:8080/openbravo/org.openbravo.service.json.jsonrest/ADUser?_where=username%3D%27Openbravo%27

谁能告诉我为什么?我不知道发生了什么...这在 1.3 上运行完美

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    你可以,

    将您的基本网址设为http://localhost:8080

    在你的控制器类中,

    public interface RestController {
    
        @GET("/openbravo/org.openbravo.service.json.jsonrest/ADUser")
        Whatever getData(@Path("_where") String whatever);
    
    }
    

    【讨论】:

    • 这是我试图避免的,但它是我现在看到的唯一解决方案......谢谢
    • @CarlosAndres:在 Retrofit2 中,您需要在基本 URL 的末尾添加尾随 /。此外,如果您在相对 url 的开头有 /,则该 url 将带有服务器根 url,而不是任何 api 版本或相对 url。你可以从杰克那里看到这个答案:stackoverflow.com/questions/32352159/…
    【解决方案2】:

    您需要在基本网址的末尾添加一个尾随 /。否则改造会忽略它。见https://github.com/square/retrofit/issues/1049

    Retrofit.Builder builder = new Retrofit.Builder()
                    .baseUrl("http://localhost:8080/openbravo/org.openbravo.service.json.jsonrest/")
                    .client(httpClient)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(gson));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2016-01-13
      相关资源
      最近更新 更多