我是新手使用Retrofit并从Retrofit 2.1.0开始。
我得到200止,但在我的call.enqueue
总是运行onFailure()
,逻辑上它必须运行onResponse()
。我真的不知道为什么,并搜索相同的问题,但他们的解释不能解决我的问题。有人可以挽救我的一天......?
我有一个带有API
的数据库的POST@FormUrlEncoded
@POST("/api/v1/meetups/")
Call<Result<Meeting>> createMeeting(@Field("name") String name,
@Field("date_start") String date_start,
@Field("date_ended") String date_ended,
@Field("location") String location,
@Field("latitude") Double latitude,
@Field("longitude") Double longitude,
@Field("points") Integer points,
@Field("member_ids") String member_ids);
这就是我在我的活动中运行的方式
Call<Result<Meeting>> call = mClient.createMeeting(meeting.getName(), meeting.getDate_start(), meeting.getDate_ended(),
meeting.getLocation(), meeting.getLatitude(), meeting.getLongitude(), meeting.getPoints(), meeting.getMember_ids());
Log.e("URL",String.valueOf(call.request().hashCode()));
call.enqueue(new Callback<Result<Meeting>>() {
@Override
public void onResponse(Call<Result<Meeting>> call, Response<Result<Meeting>> response) {
Log.e("Response Code", String.valueOf(response.code()));
if (response.isSuccessful()) {
Log.e("Meeting ", "Dibuat");
Toast.makeText(getApplicationContext(), "Meeting Created", Toast.LENGTH_SHORT).show();
Intent i = new Intent(mContext, MainActivity.class);
mContext.startActivity(i);
((Activity) mContext).finish();
} else {
Toast.makeText(getApplicationContext(), "Create Meeting Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Response Error: HTTP Code " + String.valueOf(response.code()),
Toast.LENGTH_SHORT).show();
Log.e(TAG + "onResponse", String.valueOf(response.raw().toString()));
}
}
@Override
public void onFailure(Call<Result<Meeting>> call, Throwable t) {
Log.e("On Failure", "Working");
}
});
这是我的会议模型
public class Meeting {
private String _id;
private String name;
private String date_start;
private String date_ended;
private String status;
private String location;
private String member_ids;
private Double latitude;
private Double longitude;
private Integer points;
private Group group;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
...
...
}
问题是当我故意在会议中输入错误的属性时,它返回错误代码500并且onResponse()
正在运行,给response.isSuccessful() == false
和Log.e()
输出是&#34;创建会议失败&#34 ;.但是当我输入正确的属性时,它会在我的数据库中提供200 OK,POST,但onFailure()
正在运行并运行Log.e()
&#34;失败:工作&#34;。
答案 0 :(得分:0)
这些步骤可能会对您有所帮助:
将调用参数调整为响应,例如,如果您的响应如下:
{
"success": true,
"data": [
{
"_id": "59cb12c0abb4713d6632c6ef",
"name": "Meeting 1",
"date_start": "2609-03-10T13:02:40.000Z",
"date_ended": "2609-03-10T13:03:00.000Z",
...
}
]
}
并使用结果的自定义模型,您的通话将是这样的:
Call<Result<List<Meeting>>> call
对于IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
的问题,您可以更改以下API代码:
@FormUrlEncoded
@POST("/api/v1/meetups/")
Call<Result<List<Meeting>>> createMeeting(@Field("name") String name, @Field("date_start") String date_start, @Field("date_ended") String date_ended, @Field("location") String location, @Field("latitude") Double latitude, @Field("longitude") Double longitude, @Field("points") Integer points, @Field("member_ids") String member_ids);
你可以像这样使用你的电话:
Call<Result<List<Meeting>>> call = mClient.createMeeting(meeting.getName(), meeting.getDate_start(), meeting.getDate_ended(), meeting.getLocation(), meeting.getLatitude(), meeting.getLongitude(), meeting.getPoints(), meeting.getMember_ids());
call.enqueue(new Callback<Result<List<Meeting>>>() {
@Override
public void onResponse(Call<Result<List<Meeting>>> call, Response<Result<List<Meeting>>> response) {
if (response.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Meeting Dibuat", Toast.LENGTH_SHORT).show();
Intent i = new Intent(mContext, MainActivity.class);
mContext.startActivity(i);
((Activity) mContext).finish();
} else {
Toast.makeText(getApplicationContext(), "Membuat Meeting Gagal", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Response Error: HTTP Code " + String.valueOf(response.code()),
Toast.LENGTH_SHORT).show();
Log.e(TAG + "onResponse", String.valueOf(response.raw().toString()));
}
}
@Override
public void onFailure(Call<Result<List<Meeting>>> call, Throwable t) {
Log.e("On Failure", t.getMessage());
}
});
请注意,您必须将Result<Meeting>
更改为Result<List<Meeting>>