我正在我的Android应用程序中实现用户登录的改造。输入类型是Json。每当我尝试通过Retrofit发送POST请求时,我就有500个内部服务器错误。
ApiInterface apiInterface=AppController.GetRetrofitObject().create(ApiInterface.class);
// prepare call in Retrofit 2.0
try {
JSONObject paramObject = new JSONObject();
try {
paramObject.put(ApiName.USERNAME, username);
paramObject.put(ApiName.PASSWORD, password);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG,paramObject.toString());
Call<Login> call = apiInterface.getLoginDetails(paramObject.toString());
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
String message = response.message();
int status = response.code();
Log.d(TAG, String.valueOf(status));
Intent homeIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(homeIntent);
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
}
});
}catch (Exception e){
e.printStackTrace();
}
每次我获得500的状态。但我在POSTMAN中获得响应。我的接口类是
public interface ApiInterface {
@POST(ApiName.LOGIN)
Call<Login> getLoginDetails(@Body String body);
}
答案 0 :(得分:0)
将String body
更改为Java pojo。例如:User
@POST(ApiName.LOGIN)
Call<Login> getLoginDetails(@Body User body);
}
.......
class User {
private String username;
private String password;
public User() {}
public User(String username, String password) {
this.username = username;
this.password = password;
}
// setter + getter//
}
然后致电:
....
User user = new User(username, password);
Call<Login> call = apiInterface.getLoginDetails(user);
....