John Davidson

java - in php, how to retrieve an objects array sent by retrofit

0 comments
Message:


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;
}
}


My Answer:
uploadBackup(@Field("backupData[]") List backupData);
In your PHP script, you can retrieve the array of objects sent by Retrofit using the following code:
php
$backupData = $_POST['backupData'];
foreach ($backupData as $backupObject) {
// Access the properties of the BackupObject
$property1 = $backupObject['property1'];
$property2 = $backupObject['property2'];
// Do something with the properties
}

Make sure to properly handle the data received from the Retrofit request in your PHP script to avoid any security vulnerabilities.

Rate this post

5 of 5 based on 9259 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us