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

时间:2014-11-23 09:38:56

标签: php android mysql json arrays

我想从android发送值数组到php插入MySQL数据库。我在这里使用数组列表。我应该使用JSONObject吗?如果是,如何通过JSON发送数组值?朋友请帮帮我

我的代码是

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] ;

}

2 个答案:

答案 0 :(得分:0)

您可以使用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();
        }

答案 1 :(得分:0)

您可以这样发送:

JSONObject jObject = new JSONObject();                  
try
{
    jObject.put("emailID", "data");
}catch(Exception ex){}

并将其发送为:

jObject.toString();