Upgrade: 500 internal server errors

I have 500 internal server errors, every time I try to send a POST request via Retrofit. When I submit a GET request, it sends correctly. I am sure that everything is in order with the servers. What is wrong with my code?

    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));
            }
        });
    }

Request declaration:

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

Examination of the request and response from the postman: enter image description here

+4
source share
1 answer

It seems that in the postman you are sending an FriendModel array, but in your code you are sending a single object.

Just change the object you are sending and send the list instead of sending one object as the server is expecting

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

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

    friendsList.add(ff);

You must 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);
+5
source

Source: https://habr.com/ru/post/1609279/


All Articles