Java 改造:500 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32821102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:09:21  来源:igfitidea点击:

Retrofit: 500 internal server error

javaandroidrestretrofit

提问by neustart47

I have 500 internal server error, every time when i try to send POST request via Retrofit. When i sending GET request, it sending correctly. I'm sure that with serverside everyting is ok. What's wrong with my code ?

每次我尝试通过 Retrofit 发送 POST 请求时,我都有 500 个内部服务器错误。当我发送 GET 请求时,它发送正确。我确信服务器端一切正常。我的代码有什么问题?

    String ENDPOINT = "http://52.88.40.210";
    //model for request
        FriendModel ff = new FriendModel();
        ff.setFriendNumber("380935275259");
        ff.setId(516);
        ff.setNumber("380936831127");

        RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(ENDPOINT)
                .build();
        WayfAPI api = adapter.create(WayfAPI.class);
        api.getFriendsLocation(ff, new Callback<List<FriendLocationModel>>() {
            @Override
            public void success(List<FriendLocationModel> friendLocationModels, Response response) {
                for (FriendLocationModel ff : friendLocationModels) {
                    Log.d("myLogs", "===========Successful==========");
                    Log.d("myLogs", "Id: " + ff.getId());
                    Log.d("myLogs", "Number: " + ff.getNumber());
                    Log.d("myLogs", "GeoLocation: : " + ff.getGeoLocation());
                }
            }

            @Override
            public void failure(RetrofitError error) {
                Log.d("myLogs", "-------ERROR-------");
                Log.d("myLogs", Log.getStackTraceString(error));
            }
        });
    }

Declaration of request:

请求声明:

@Headers({
        "Accept: application/json",
        "Content-type: application/json"
})
@POST("/api/geo/getLoc")
public void getFriendsLocation(@Body FriendModel friendModel, Callback<List<FriendLocationModel>> response);

Exampe of request and response from Postman: enter image description here

Postman 的请求和响应示例: 在此处输入图片说明

采纳答案by Udi Idan

It seems that in postman you're sending an array of FriendModel, but in your code you're sending a single object.

似乎在邮递员中您发送的是一个 FriendModel 数组,但在您的代码中您发送的是一个对象。

Just change the object you're sending, and instead of sending a single object, send a List as the server expects

只需更改您发送的对象,而不是发送单个对象,而是按照服务器的预期发送列表

    List<FriendModel> friendsList = new ArrayList<FriendModel>();

    FriendModel ff = new FriendModel();
    ff.setFriendNumber("380935275259");
    ff.setId(516);
    ff.setNumber("380936831127");

    friendsList.add(ff);

You should also change this signature:

您还应该更改此签名:

public void getFriendsLocation(@Body FriendModel friendModel, Callback<List<FriendLocationModel>> response);

to

public void getFriendsLocation(@Body List<FriendModel> friendModel, Callback<List<FriendLocationModel>> response);