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

如何通过Volley传递ArrayList至PHP脚本且无需转JSON?现有方法仅支持字符串

Pass ArrayList to PHP via Volley Without Converting to JSON

Hey there, I get that you want to send an ArrayList to your PHP script using Volley without converting it to JSON, and your current setup only handles strings. Let's fix that—here's a straightforward solution that works for basic type ArrayLists (like String, Integer) by leveraging HTTP form parameters in a way PHP natively understands:

Android (Volley) Implementation

Instead of serializing the entire ArrayList to JSON, you can send each element as a parameter with a key formatted like items[]. PHP automatically parses these into an array, so no JSON parsing is needed on either end.

Here's a complete code example:

// Your sample ArrayList
ArrayList<String> myItemList = new ArrayList<>();
myItemList.add("First Item");
myItemList.add("Second Item");
myItemList.add("Third Item");

// Your PHP script URL
String phpEndpoint = "http://your-server-address/handle-items.php";

// Create a POST request with Volley
StringRequest postRequest = new StringRequest(Request.Method.POST, phpEndpoint,
        response -> {
            // Success callback: handle the server's response
            Log.i("VolleySuccess", "Server response: " + response);
        },
        error -> {
            // Error callback: handle network issues
            Log.e("VolleyError", "Request failed: " + error.getMessage());
        }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        
        // Add each item from the ArrayList to the params with the key "items[]"
        for (String item : myItemList) {
            params.put("items[]", item);
        }
        
        // Optional: if you need to send additional string parameters, add them here
        // params.put("user_id", "123");
        
        return params;
    }
};

// Add the request to Volley's queue
Volley.newRequestQueue(getApplicationContext()).add(postRequest);

PHP Script Implementation

On the PHP side, you can directly access the sent items as an array using $_POST['items']:

<?php
// Check if the items parameter exists in the POST request
if (isset($_POST['items']) && is_array($_POST['items'])) {
    $receivedItems = $_POST['items'];
    
    // Process each item (example: print them out)
    foreach ($receivedItems as $index => $item) {
        echo "Item " . ($index + 1) . ": " . htmlspecialchars($item) . "\n";
    }
} else {
    echo "No items received or invalid format.";
}
?>

Why This Works

When you use the key items[] for multiple parameters in a POST request, PHP recognizes this as an array input. Volley handles packaging these key-value pairs into standard form data (application/x-www-form-urlencoded), which PHP parses automatically—no JSON encoding/decoding required.

Note for Custom Object ArrayLists

If your ArrayList contains custom objects (not basic types like String/Integer), this method won't work directly (since you can't send objects as form parameters). In that case, you'd either need to break down each object into individual fields (e.g., object_0_name, object_0_value) or use JSON serialization—but since you asked to avoid JSON, this solution is best suited for basic type lists.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:11:13