改进从POST调用中获取空参数的方法

时间:2019-06-03 13:22:49

标签: android post retrofit

我正在使用改造来在我的android应用程序中进行API调用。一切都可以很好地处理GET请求,但是当我使用POST方法调用API并使用@Body注释将自定义对象传递给它时,API不会t接收到传递的参数。我在POSTMAN中使用相同的值测试了相同的API,并得到了响应。我已经使用了Retrofit一段时间,但在这种情况下却无法确切了解发生的事情。感谢您的帮助。

ApiInterface.java

@POST("User/login")
@Headers("x-api-key: 123456")
Call<LoginResponse> login(@Body LoginRequest loginRequest);

LoginRequest.java

public class LoginRequest {

@SerializedName("email")
@Expose
public String email;
@SerializedName("password")
@Expose
public String password;
@SerializedName("deviceType")
@Expose
public Integer deviceType;
@SerializedName("deviceId")
@Expose
public String deviceId;
@SerializedName("versionName")
@Expose
public String versionName;

public LoginRequest() {
}

public LoginRequest(String email, String password, Integer deviceType, String deviceId, String versionName) {
    super();
    this.email = email;
    this.password = password;
    this.deviceType = deviceType;
    this.deviceId = deviceId;
    this.versionName = versionName;
}
    //getters and setters

}

LoginResponse.java

public class LoginResponse {

@SerializedName("userId")
@Expose
private String userId;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("middleName")
@Expose
private Object middleName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("userCompany")
@Expose
private String userCompany;
@SerializedName("userName")
@Expose
private String userName;
@SerializedName("userDesignation")
@Expose
private String userDesignation;
@SerializedName("companyName")
@Expose
private String companyName;
@SerializedName("photo")
@Expose
private Object photo;
@SerializedName("userType")
@Expose
private String userType;
@SerializedName("code")
@Expose
private String code;
@SerializedName("countryCode")
@Expose
private String countryCode;
@SerializedName("countryName")
@Expose
private String countryName;
@SerializedName("supervisor")
@Expose
private String supervisor;
@SerializedName("taskStatus")
@Expose
private Integer taskStatus;
@SerializedName("checkInStatus")
@Expose
private Integer checkInStatus;
@SerializedName("time")
@Expose
private String time;
@SerializedName("checkInPlace")
@Expose
private String checkInPlace;
@SerializedName("checkInId")
@Expose
private String checkInId;
@SerializedName("checkinDate")
@Expose
private String checkinDate;
@SerializedName("permission")
@Expose
private List<Permission> permission = null;

public LoginResponse() {
}

public LoginResponse(String userId, String firstName, Object middleName, String lastName, String email, String phone, String userCompany, String userName, String userDesignation, String companyName, Object photo, String userType, String code, String countryCode, String countryName, String supervisor, Integer taskStatus, Integer checkInStatus, String time, String checkInPlace, String checkInId, String checkinDate, List<Permission> permission) {
    super();
    this.userId = userId;
    this.firstName = firstName;
    this.middleName = middleName;
    this.lastName = lastName;
    this.email = email;
    this.phone = phone;
    this.userCompany = userCompany;
    this.userName = userName;
    this.userDesignation = userDesignation;
    this.companyName = companyName;
    this.photo = photo;
    this.userType = userType;
    this.code = code;
    this.countryCode = countryCode;
    this.countryName = countryName;
    this.supervisor = supervisor;
    this.taskStatus = taskStatus;
    this.checkInStatus = checkInStatus;
    this.time = time;
    this.checkInPlace = checkInPlace;
    this.checkInId = checkInId;
    this.checkinDate = checkinDate;
    this.permission = permission;
}

//getters and setters
}

ApiClient.java

public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

POSTMAN屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

 @FormUrlEncoded
 @Headers("x-api-key: 123456")
 @POST("User/login")
 Call<LoginResponse> login(@Field("email") email, @Field("password") password,@Field("deviceType") deviceType,@Field("deviceId") deviceId,@Field("versionName") versionName);

答案 1 :(得分:0)

在Postman中,您发送的是form-data而不是raw,这就是API返回响应的原因。在Retrofit中,您正在发送raw JSON请求。我认为该API接受multipart/form-data。只需像这样调整API接口,

import okhttp3.RequestBody;
import retrofit2.http.PartMap;
import retrofit2.http.Multipart;

@Multipart        //   <====== add this line
@POST("User/login")
@Headers("x-api-key: 123456")
Call<LoginResponse> login(@PartMap Map<String, RequestBody> params);  // change parameters 

然后在活动/片段中调整代码

Map<String, RequestBody> params = new HashMap<>();
params.put("email", RequestBody.create(MultipartBody.FORM, email));
params.put("password", RequestBody.create(MultipartBody.FORM, password));
params.put("deviceType", RequestBody.create(MultipartBody.FORM, String.valueOf(deviceType)));
params.put("deviceId", RequestBody.create(MultipartBody.FORM, deviceId));
params.put("versionName", RequestBody.create(MultipartBody.FORM, versionName));

YourApiInterface api = ...
Call<LoginResponse> call = api.login(params);   // pass it to method
call.enqueue(/* implementation */);

请检查(并了解)form-data, x-www-form-urlencoded and raw

之间的区别

注意:请正确检查导入语句和代码注释。