1

In my android app, with retrofit, I want to send an array of objects to my php script with:

@FormUrlEncoded
@POST(Consts.BACKUP_SCRIPT_UP)
Call<Integer> backupUpload(
        @Field("deviceName") String deviceName,
        @Field("articles[]") List<backupArticle> articles
);

But, I don't find the way to retrieve my datas. I get deviceName as attended but how to retrieve datas in the array $_REQUEST["articles"] please?

Dump gives something like:

Array (
    [deviceName] => 7cdb101d51bb89ca
    [articles] => Array
        (
            [0] => myapp.backupArticle@f3ee606
            [1] => myapp.backupArticle@681f4c7
            [2] => myapp.backupArticle@6d40af4
            [3] => myapp.backupArticle@57321d
            [4] => myapp.backupArticle@5d35c92
            ...
        )
)

But I don't really know if my problem is about java/android or php. I'd like to be sure what I send is really what I want to send, but how to test it if I can't retrieve information in php?

I also tried with:

@POST(Consts.BACKUP_SCRIPT_UP)
Call<Integer> backupUpload(
        @Body List<backupArticle> articles
);

but I get empty array in php.

java object Class:

public class backupArticle {
    @SerializedName("deviceName") @Expose private String deviceName;
    @SerializedName("clistName") @Expose private String clistName;
    @SerializedName("name") @Expose private String name;
    @SerializedName("ord") @Expose private long ord;
    @SerializedName("categName") @Expose private String categName;
    @SerializedName("categOrd") @Expose private long categOrd;
    @SerializedName("qty") @Expose private int qty;
    @SerializedName("selected") @Expose private int selected;

    public backupArticle(String deviceName, String clistName, String name, long ord, String categName, long categOrd, int qty, int selected) {
        this.deviceName = deviceName;
        this.clistName = clistName;
        this.name = name;
        this.ord = ord;
        this.categName = categName;
        this.categOrd = categOrd;
        this.qty = qty;
        this.selected = selected;
    }
    public String getDeviceName() {
        return deviceName;
    }
    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }
    public String getClistName() {
        return clistName;
    }
    public void setClistName(String clistName) {
        this.clistName = clistName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getOrd() {
        return ord;
    }
    public void setOrd(long ord) {
        this.ord = ord;
    }
    public String getCategName() {
        return categName;
    }
    public void setCategName(String categName) {
        this.categName = categName;
    }
    public long getCategOrd() {
        return categOrd;
    }
    public void setCategOrd(long categOrd) {
        this.categOrd = categOrd;
    }
    public int getQty() {
        return qty;
    }
    public void setQty(int qty) {
        this.qty = qty;
    }
    public int getSelected() {
        return selected;
    }
    public void setSelected(int selected) {
        this.selected = selected;
    }
}
Chris972
  • 71
  • 1
  • 8
  • @chiliNUT Yes. They are present but they are objects, And I don't know how to access to their properties. – Chris972 Oct 26 '22 at 14:48
  • [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – ADyson Oct 26 '22 at 14:49
  • yeah, sorry, I didn't read your question close enough so I deleted that comment. I'm not familiar with retrofit in particular, but in general this looks like you need to make sure your article class is serializable – chiliNUT Oct 26 '22 at 14:50
  • @ADyson yes, but when I do foreach ($_REQUEST["articles"] as $article) { $req .= $article->name."\n"; } All is empty – Chris972 Oct 26 '22 at 14:57
  • @chiliNUT they should be – Chris972 Oct 26 '22 at 14:58
  • @chiliNUT I completed my question. Should I add something else please? – Chris972 Oct 26 '22 at 15:45
  • If your object is serializable, It looks like retrofit isn't serializing your objects correctly when its posting to your php endpoint. I think your question looks good – chiliNUT Oct 26 '22 at 16:56

1 Answers1

0

In fact, it seems that the right way is to NOT use $_REQUEST (or $_POST) but (php):

$articles = json_decode(file_get_contents('php://input'));
$deviceName = $articles[0]->deviceName;

with (java/retrofit):

@POST(Consts.BACKUP_SCRIPT_UP)
Call<Integer> backupUpload(
        @Body List<backupArticle> articles
);
Chris972
  • 71
  • 1
  • 8