2

I need help with sending @POST request. Right now my post method looks like this :

@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);

The problem is with array sleep_mode. When I send it as I wrote I'm sending data like this : RESPONSE_UNCORRECT

While I need to send in format like that :

enter image description here

I tried to do it with @Body but it hasn't worked then. ANy help please ? Thanks in advance. My 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;

--------------------------- EDIT , VERSION WITH "@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;

This is how i create callback :

@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);


    }

Validate class :

public class ValidateNewBeaconDataCommand extends ApiCommand {
   public EonBeaconModel model;


    public ValidateNewBeaconDataCommand(EonBeaconModel model) {

       this.model=model;
    }
}

and this is how I create a request :

EventBus.post(new ValidateNewBeaconDataCommand(beaconModel));

THANKS and sorry for so much code :)

2
  • post your sleepmode class too Commented Dec 14, 2016 at 11:59
  • @Nas I added it to the post. Commented Dec 15, 2016 at 10:10

3 Answers 3

1
instead of Field use FieldMap

@FieldMap Map<String, String> model;

put your data with your key("sleep_mode[0]") in map
Sign up to request clarification or add additional context in comments.

6 Comments

you mean : @FieldMap List<Map<String, String>> model, ?
@FieldMap parameter type must be Map <-- can't send List of Maps
then @Field ("sleep_modes[]") List<Map<String, String>> model will work for you
Done with : @FieldMap Map<String,String> fields, and then : Map<String, String> fields = new HashMap<>(); for (int i = 0; i < 3; i++) { SleepMode mode1 = new SleepMode("13:00", "14:00", "1"); fields.put("sleep_mode[" + i + "][turn_on]", mode1.turn_on); fields.put("sleep_mode[" + i + "][time_from]", mode1.time_from); fields.put("sleep_mode[" + i + "][time_to]", mode1.time_from);
Yes it does:) Thanks!
|
1

You should try @Body annotation with something like that

@FormUrlEncoded
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(@Body BodyRequest request);

And create a BodyRequest class with all your arguments :

class BodyRequest {
  long beaconId;
  String password;
  // ....
}

7 Comments

I did as you suggest but right now app doesn't send anything to API ;/ I can edit my post if you like.
I eddited the post :)
And you still have @FormUrlEncoded on your method ?
No I don't. I had to delete it cause it caused an exception
Which exception ?
|
1

Do it like this

@FormUrlEncoded
@POST("/api/mobile/{language}/{apiVersion}/beacons/try-save-settings")
void trySaveBeaconParams(@Body HashMap<String, Object> map);

and then you can put anything as value of in that 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); 

you can even add the list or array inside that map

3 Comments

thanks for help but when I create map , add fields to it as you suggest server returns null at every field. Nothing is being sent :/
in void trySaveBeaconParams(@Body HashMap<String, Object> map); instead of "Body" try using "FieldMap"
the same problem :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.