接收json数组到PHP

时间:2014-05-19 08:19:39

标签: php android json

我在android中传递json数组如下。这是下面的代码

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", request_tag));

    if(data.moveToFirst())
    {
        do
        {
            params.add(new BasicNameValuePair("iscomplete[]",data.getString(0)));
            params.add(new BasicNameValuePair("uidUser[]",data.getString(1)));
            params.add(new BasicNameValuePair("connectID[]",data.getString(2)));
        }while(data.moveToNext());
    }
response = jsonParser.getJSONFromUrl(requestUrl, params);
    Toast.makeText(context, response, 10000).show();

现在我想在服务器端接收它,它不会在php上工作

if ($tag ==  'request')
{
    $complete = (array)$_POST['iscomplete[]'];
    $uiuser = (array)$_POST['uidUser[]'];
    $connectId = (array)$_POST['connectID[]'];
               echo $complete//This gives a blank value
}

json代码如下

public String getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        response=EntityUtils.toString(httpEntity);

        Log.d("Response",response);
        //jObj=new JSONObject(response);
    } catch (Exception e) {
        e.printStackTrace();
    } 


    return response;

}

在回复吐司时,我收到一条空信息。我想我没有正确收到数据

1 个答案:

答案 0 :(得分:1)

在您的Android应用中,您可以通过在名称声明后给它们方括号来创建iscomplete,uidUser和connectID param作为数组。

当PHP解释页面时,它会将所有参数(无论是POST还是GET)转换为基于数组的等价物。所以

?iscomplete[]=one&uidUser=23&connectID=3432

将成为:

$_POST['iscomplete'] = array('one');
$_POST['uidUser'] = array(23);
$_POST['connectID'] = array(3432);

当你想获得值时,你不需要包括方括号 - PHP已经剥离了它们。所以你只需要:

if ($tag ==  'request')
{
    $complete = (array)$_POST['iscomplete'];
    $uiuser = (array)$_POST['uidUser'];
    $connectId = (array)$_POST['connectID'];
    echo $complete;
}