繁体   English   中英

响应体总是 null 在 android 来自改造 2 api 请求

[英]Response body always null in android from retrofit2 api request

I'm developing an android app with android studio, and I have a rest api, which I developed with spring. 每次我使用 retrofit 从服务器检索某些东西时,我都会得到 null 正文。 即使我的服务器发送数据。

我已经尝试过 ScalarsConverterFactory 和 GsonConverterFactory,我也尝试将 Call 的类型更改为 String、ResponseBody 和 JsonObject

我在这里打电话:

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("my_url")
                .build();

ServerService service = retrofit.create(ServerService.class);
Call<List<String>> call = service.getUserAlbums("addcookie");
Response<List<String>> response = call.execute();

服务器服务.java

@GET("/cmu/getUserAlbums")
Call<List<String>> getUserAlbums(@Header("Cookie") String cookie);

这是服务器中的代码

@RequestMapping(value = "/getUserAlbums", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.FOUND)
    public List<String> getUserAlbums() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        User user = userService.getUserByUsername(auth.getName());
        return userService.getUserByUsername(user.getUsername()).getAlbums();

    }

我期待得到一个字符串形式的列表,我得到 HTTP 代码 302,在我的控制台中我可以看到服务器返回了列表,但我无法在 android 上接收。

我解决了这个问题,如果代码介于200-299之间,则改造基本上会认为响应成功,因此在返回302时,我正在寻找的响应主体位于.errorBody()中。

发生错误是因为您在 controller 中返回状态代码302 FOUND 检索数据时,您应该返回200 OK

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM