1

I want to send an array of strings to the server from an Android app using Retrofit. I have no idea how can I send it and receive it in server side:

//api interface...

@FormUrlEncoded
@POST("sendArray.php")
Call<ResponseModel> sendAns(@Field("ans[]") String[] ans);

//sending array in main activity

String[] ans = {"ans1","ans2","ans3"};
Call<ResponseModel> call = apiObject.sendAns(ans);
call.en....

//server side php code to get array

$ans = $_POST['ans[]'];
$ans1 = $ans[1];

I expected the value of $ans1 = "ans2" but we got nothing.

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

send and receive array like this:

  @POST("sendArray.php")
  Call<ResponseModel> getSomething(@Body String[] ans );

And if you want to name your array then create RequestModel with array inside named as you want. Also you can set name using @SerializedName annotation

public class YourBodyRequest {
    @SerializedName("ans[]")
    public String[] ans;
}

And:


 @POST("sendArray.php")
 Call<ResponseModel> request(@Body YourBodyRequest request );
Antonis Radz
  • 3,036
  • 1
  • 16
  • 34