Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

java - Retrofit: 500 internal server error

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 ?

    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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...