我需要帮助发送@POST请求。现在我的post方法看起来像这样:
@FormUrlEncoded
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(@Field("beacon_id") long beaconId,
@Field("password") String password,
@Field("ib[turn_on]") int turn_on,
@Field("ib[minor]") int minor ,
@Field("ib[major]") int major ,
@Field("ib[tx_power]") int tx_power ,
@Field("ib[interval]") int interval,
@Field("ib[secure_uuid]") int secure_uuid,
@Field("euid[turn_on]") int euid_turn_on,
@Field("euid[interval]") int euid_interval,
@Field("euid[tx_power]") int euid_tx_power,
@Field("euid[namespace]") String euid_namespace,
@Field("euid[instance]") String euid_instance,
@Field("eeid[turn_on]") int eeid_turn_on,
@Field("eeid[interval]") int eeid_interval,
@Field("eeid[tx_power]") int eeid_tx_power,
@Field("eurl[turn_on]") int eurl_turn_on,
@Field("eurl[interval]") int eurl_interval,
@Field("eurl[tx_power]") int eurl_tx_power,
@Field("eurl[url]") String eurl_url,
@Field("etlm[turn_on]") int etlm_turn_on,
@Field("etlm[interval]") int etlm_interval,
@Field("etlm[tx_power]") int etlm_tx_power,
@Field ("sleep_modes[]") List<SleepMode> model,
Callback<BasicResponse<Object>> callback);
问题在于数组sleep_mode。当我发送它时,我发送的数据是这样的:
虽然我需要发送这样的格式:
我试图用@Body做到这一点,但它还没有奏效。请帮忙吗?提前致谢。 我的SleepMode.class:
@Table(name = "SleepMode")
public class SleepMode {
@Column
@Expose
@SerializedName("turn_on")
public int turn_on;
@Column
@Expose
@SerializedName("time_from")
public String time_from;
@Column
@Expose
@SerializedName("time_to")
public String time_to;
---------------------------编辑,版本与“@BODY”------------ -------------
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(
@Body EonBeaconModel model,
Callback<BasicResponse<Object>> callback);
EonBeacon.class
@Table(name = "eonBeacon")
public class EonBeaconModel extends Model {
@Column(unique = true, onUniqueConflict = Column.ConflictAction.REPLACE, index = true)
public long beacon_id;
@Column
public long bridge_id;
@Column
public long mesh_id;
@Column
public String name;
@Column
public String location;
@Column
public String mac_address;
@Column
public String firmware;
@Column
public String pcb_revision;
@Column
public int battery_level;
@Column
public String password;
@Column
public String assigned_bridge_id;
@Column(name="Eeid")
public Eeid eeid;
@Column(name="Etlm")
public Etlm etlm;
@Column
public Euid euid;
@Column(name="Eurl")
public Eurl eurl;
@Column(name="Ib")
public IbModel ibBeaconModel;
@Column
public String jsonIB;
@Column
public String jsonEeid;
@Column
public String jsonEtlm;
@Column
public String jsonEuid;
@Column
public String jsonEurl;
@Column
public boolean isBeacon;
@Column(name="sleep_modes")
public List<SleepMode> sleep_modes;
@Column
public String jsonSleep;
这是我创建回调的方式:
@Subscribe
public void validateNewBeaconData(ValidateNewBeaconDataCommand command) {
Callback<BasicResponse<Object>> callback = new Callback<BasicResponse<Object>>() {
@Override
public void success(BasicResponse<Object> basicResponse, Response response) {
if (ResponseStatus.SUCCESS.statusCode == basicResponse.status) {
EventBus.post(new BeaconsSettingsValidatedEvent());
Log.e(TAG, "success: ");
}
else {
EventBus.post(new NetworkFailureEvent(ResponseStatus.getByStatus(basicResponse.status), basicResponse.status_msg));
Log.e(TAG, "failure validate: " );
}
}
@Override
public void failure(RetrofitError error) {
EventBus.post(new NetworkFailureEvent(ResponseStatus.NETWORK_ERROR, messageString));
Log.e(TAG, "failure: " + error.getCause() );
}
};
service.trySaveBeaconParams(command.model
,
callback);
}
验证课程:
public class ValidateNewBeaconDataCommand extends ApiCommand {
public EonBeaconModel model;
public ValidateNewBeaconDataCommand(EonBeaconModel model) {
this.model=model;
}
}
这就是我创建请求的方式:
EventBus.post(new ValidateNewBeaconDataCommand(beaconModel));
感谢和抱歉这么多代码:)
答案 0 :(得分:1)
您应该尝试使用类似
的@Body注释@FormUrlEncoded
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(@Body BodyRequest request);
使用所有参数创建一个BodyRequest类:
class BodyRequest {
long beaconId;
String password;
// ....
}
答案 1 :(得分:1)
这样做
@FormUrlEncoded
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(@Body HashMap<String, Object> map);
然后你可以把任何东西作为该地图的价值
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("beacon_id" ,beaconId);
map.put("beacon_id" ,beaconId);
map.put("password" , password);
map.put("ib[turn_on]" , turn_on);
map.put("ib[minor]" , minor);
map.put("ib[major]" , major);
您甚至可以在该地图中添加列表或数组
答案 2 :(得分:1)
instead of Field use FieldMap
@FieldMap Map<String, String> model;
put your data with your key("sleep_mode[0]") in map