我想处理来自服务器的响应,但我不知道JSON(来自服务器)的外观如何。所以我试着将响应显示为String,但我无法做到。是否可以将响应显示为String?然后正确处理响应。感谢
(改造1.9)
LoginService loginService = RetrofitClient.createService(LoginService.class);
loginService.searchOffer(getToken(), offerSearch, new Callback<String>() {
@Override
public void success(String offerSearchRequestResource, Response response) {
String responseFromSercer = response.getBody();
}
@Override
public void failure(RetrofitError error) {
}
});
答案 0 :(得分:1)
将响应模型更改为
JSONObject(来自Gson)
然后在您的
public void success(...){response.body.toString();}
像这样:
Call<JsonObject> call = YOUR_API_INTERFACE().YOUR_METHOD(YOUR_PARAMS);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, @NonNull Response<JsonObject> response) {
if(response.isSuccessful()) {
String responseFromSercer = response.body.toString();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
.....
}
});
答案 1 :(得分:0)