改造:500个内部服务器错误
问题描述:
每次尝试通过翻新发送POST请求时,我都有500个内部服务器错误.当我发送GET请求时,它发送正确.我敢肯定,在服务器端,一切都可以.我的代码有什么问题?
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));
}
});
}
请求声明:
@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:
答
似乎在邮递员中,您发送的是FriendModel数组,但是在代码中,您的发送的是单个对象.
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);
您还应该更改此签名:
public void getFriendsLocation(@Body FriendModel friendModel, Callback<List<FriendLocationModel>> response);
到
public void getFriendsLocation(@Body List<FriendModel> friendModel, Callback<List<FriendLocationModel>> response);