数组作为在改型参数

问题描述 投票:0回答:2

我有一个问题,我的参数传递改造,我的问题是需要发送int数组([3,1,2]),作为与改造2 POST方法的参数之一,其他参数都为字符串。 (例如:提示 - “10”,amount- “100”,服务的id - [3,1,2])。例如,在如何发送参数,如上述。

android arrays retrofit
2个回答
2
投票

您可以使用ArrayList如:

@FormUrlEncoded
    @POST("service_name") 
       void functionName(
            @Field("yourarray[]") ArrayList<String> learning_objective_uuids, @Field("user_uuids[]") ArrayList<String> user_uuids, @Field("note") String note,
            Callback<CallBackClass> callback
        );

您可以按照link

或者你可以使用的JSONObject像这样:

@POST("demo/rest/V1/customer")
Call<RegisterEntity> customerRegis(@Body JsonObject registrationData);

registrationData:

private static JsonObject generateRegistrationRequest() {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject subJsonObject = new JSONObject();
            subJsonObject.put("email", "[email protected]");
            subJsonObject.put("firstname", "abc");
            subJsonObject.put("lastname", "xyz");

            jsonObject.put("customer", subJsonObject);
            jsonObject.put("password", "password");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonParser jsonParser = new JsonParser();
        JsonObject gsonObject = (JsonObject) jsonParser.parse(jsonObject.toString());
        return gsonObject;
    }

0
投票

可以定义反映POST体的结构的对象:

@POST("/pathtopostendpoint")
Call<ResponseObject> postFunction(@Body final RequestBody body);

RequestBody被定义为以下(如果使用GSON转换器,调整与@SerializedName领域的命名):

class RequestBody {

    String tips;
    String amount;
    int[] serviceIds;

    RequestBody(final String tips, final amount String, final int[] serviceIds) {
        this.tips = tips;
        this.amount = amount;
        this.serviceIds = serviceIds;
    }
}

并建立类似的请求调用:

final Call<ResponseObject> call = retrofitService.postFunction(
    new RequestBody("10", "100", new int[]{ 3, 1, 2 })
);
© www.soinside.com 2019 - 2024. All rights reserved.