gpt4 book ai didi

php - 如何将值数组从 android 发送到 php

转载 作者:行者123 更新时间:2023-11-29 23:24:08 25 4
gpt4 key购买 nike

我想将值数组从 android 发送到 php 以插入到 MySQL 数据库中。这里我使用的是数组列表。我应该使用 JSONObject 吗?如果是,如何通过 JSON 发送数组值? friend 们请帮助我

这是我的代码

ArrayList<NameValuePair> al = new ArrayList<NameValuePair>();

String id[]={"anu","bavi","citi" };

List<String> stfids=new ArrayList<String>();

for(int i=0;i<2;i++)

{
stfids.add(id[i]);
}

for (int i = 0; i < 2; i++)

{
al.add(new BasicNameValuePair("names["+i+"]",String.valueOf(stfids.get(i))));
}

String response = CustomHttpClient.executeHttpPost("http:\\192.168.1.1\my-folder\myfile.php",al);

String result = response.toString();

myfile.php

$arr[] = $_REQUEST['names[]'];


for($i=0;$i<2;$i++)

{

echo $arr[$i] ;

}

最佳答案

您可以使用 JSONObject,它可以将数组作为 json 元素。

例如:

JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
try {
jsonObject.put("jsonArray",jsonArray);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于php - 如何将值数组从 android 发送到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com