You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Retrofit 1.9如何从List发送多个动态@Field?

Solution for Dynamic Form Fields in Retrofit Order Submission

Got it, let's tackle this dynamic form field problem you're facing with Retrofit. The core issue is needing to send fields where the keys are pulled from your SQLite cart data (instead of hardcoding like @Field("440005")), and Retrofit has a straightforward tool for this: @FieldMap.

Step 1: Update Your Retrofit Interface

Replace those hardcoded dynamic @Field parameters with a @FieldMap that accepts a Map<String, String>. You can keep the fixed fields (table_no, waiter_no) as separate parameters or bundle them into the map—either approach works.

Option 1: Fixed Fields + Dynamic Field Map (Cleaner Separation)

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface OrderApi {
    @FormUrlEncoded
    @POST("/Order/Order")
    Call<YourOrderResponseModel> postOrder(
        @Field("table_no") String tableNumber,
        @Field("waiter_no") String waiterNumber,
        @FieldMap Map<String, String> cartItems
    );
}

Option 2: All Fields in One Map

If you prefer to bundle everything into a single map:

@FormUrlEncoded
@POST("/Order/Order")
Call<YourOrderResponseModel> postOrder(@FieldMap Map<String, String> allOrderFields);

Step 2: Build the Dynamic Map from SQLite Data

Fetch your cart entries from SQLite, then populate the map with the required key-value pairs:

  • Key: cart.getSubMenuItemCode() (e.g., "440005")
  • Value: Formatted as "{qty},{name}" (e.g., "3,Juice")
// 1. Fetch cart data from SQLite (replace with your actual database logic)
List<Cart> cartList = yourSQLiteHelper.getCartItems();

// 2. Build the dynamic field map
Map<String, String> cartItemMap = new HashMap<>();
for (Cart cart : cartList) {
    String itemCode = cart.getSubMenuItemCode();
    String itemValue = String.format("%d,%s", cart.getQty(), cart.getName());
    cartItemMap.put(itemCode, itemValue);
}

// 3. Add fixed fields here if using Option 2 (skip for Option 1)
// cartItemMap.put("table_no", "YOUR_TABLE_NUMBER");
// cartItemMap.put("waiter_no", "YOUR_WAITER_NUMBER");

Step 3: Call the API

Execute the request with your Retrofit instance. Here's a basic callback-based example:

// Initialize your Retrofit API instance (use your existing setup)
OrderApi orderApi = retrofit.create(OrderApi.class);

// Option 1 call (fixed fields + dynamic map)
Call<YourOrderResponseModel> call = orderApi.postOrder(
    "TABLE_007",
    "WAITER_12",
    cartItemMap
);

// Option 2 call (all fields in one map)
// Call<YourOrderResponseModel> call = orderApi.postOrder(cartItemMap);

// Execute the request
call.enqueue(new Callback<YourOrderResponseModel>() {
    @Override
    public void onResponse(Call<YourOrderResponseModel> call, Response<YourOrderResponseModel> response) {
        if (response.isSuccessful()) {
            // Handle successful order submission
            YourOrderResponseModel responseData = response.body();
        } else {
            // Handle API errors (e.g., 4xx/5xx status codes)
        }
    }

    @Override
    public void onFailure(Call<YourOrderResponseModel> call, Throwable t) {
        // Handle network failures
        t.printStackTrace();
    }
});

Key Notes

  • Single vs Multiple Items: This code works for both single and multiple cart entries—just add all SQLite cart items to the map, and Retrofit will automatically include each as a separate form field.
  • Formatting: Double-check that the value string matches exactly what the server expects ("{qty},{name}" with no extra spaces).
  • Response Model: Replace YourOrderResponseModel with your actual POJO that matches the server's response structure.

内容的提问来源于stack exchange,提问作者Pyae Phyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:00:42