使用 Android 异步 Http 客户端问题将 ArrayList 传递给 PHP [英] Passing ArrayList to PHP using Android Asynchronous Http Client Issue

查看:24
本文介绍了使用 Android 异步 Http 客户端问题将 ArrayList 传递给 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ArrayList 发送到 PHP ($_POST) 并将其用作数组.我使用 Android 异步 Http 客户端 http://loopj.com/android-async-http/

Im trying to send ArrayList to PHP ($_POST) and use it as an array. I use Android Asynchronous Http Client http://loopj.com/android-async-http/

我的代码

ArrayList<String> array = new ArrayList<String>();
array.add("1");
array.add("2");
array.add("3");
RequestParams params = new RequestParams();
params.put("descr_array", array );
AsyncHttpClient client = new AsyncHttpClient();
client.post(StaticValues.SERVER_PAGE, params, new AsyncHttpResponseHandler() { ... }

在 PHP 中

$descrarray[] = $_POST['descr_array'];

仅显示一项,作为字符串.

shows only one item, as a string.

$result = print_r($_POST);
print(json_encode($result));

调试代码展示

数组 ( [descr_array] => 1 )

Array ( [descr_array] => 1 )

只有一个字符串,我该如何解决?

There is only one string, how can I solve it?

推荐答案

如果你想得到所有的值写:

If you want to get all values write:

RequestParams params = new RequestParams();
params.put("descr_array_1", array.get(0) );
params.put("descr_array_2", array.get(1) );
params.put("descr_array_2", array.get(2) );

但我更喜欢你使用 Gson 将数组转换为字符串并使用一个仅对:

But I prefer you to use Gson to convert Array to String and use one pair only:

Gson gson = new Gson(); 
String out = gson.toJson(array);
params.put("descr_array", out ); 

这篇关于使用 Android 异步 Http 客户端问题将 ArrayList 传递给 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆