android - Retrofit 2.0 中的网址错误

标签 android retrofit

我有一个用 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 上完美运行

最佳答案

您需要在基本网址末尾添加尾随/。否则改造会忽略它。请参阅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));

关于android - Retrofit 2.0 中的网址错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158617/

相关文章:

android - call.enqueue 不起作用并且 response.body 在改造中为 null

android - Flutter - 另一个 Listview 中的 Listview.builder

android - 禁用 DrawerLayout 的稀松布触摸手势

android - 如何制作 4 个尺寸相同的 2x2 按钮

java - 如何使用 MVVM 和 Retrofit 将存储库实现到我的项目中

android - 使用 Retrofit 时如何构建 API

android - google books api改造

滚动时 Android ListView 样式问题与背景

android - 将 Google Sky Map 集成到应用程序中

java - Retrofit:使用 GSON 将 JSON 解析为多个 Java 对象