0

im passing json array as follows in android. Here is the code below

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();

now i want to receive it on server side bt it wont work on php

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

json code is as follows

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;

}

On echoing the toast i get an empty message. I think i have not received the data properly

5
  • Can you show us the JSON? Commented May 19, 2014 at 8:21
  • 1
    $local_array_of_object = json_decode($POST['incomingjsonarray']); Commented May 19, 2014 at 8:22
  • @Daan added new code. Commented May 19, 2014 at 8:22
  • 1
    Try $_POST['iscomplete']. If that doesn't work, dump out the content post with a var_dump($_POST) to see what's actually being passed. Commented May 19, 2014 at 8:24
  • Brilliant :) I'll create it as a proper answer :D Commented May 19, 2014 at 10:54

1 Answer 1

1

Within your Android app, you're creating the iscomplete, uidUser and connectID param's as arrays by giving them square brackets after the name declaration.

When PHP interprets a page, it converts all parameters (be it from POST or GET) into their array based equivalents. So

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

will become:

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

When you want to get the value, you don't need to include the square brackets - PHP has already stripped them. So you just need:

if ($tag ==  'request')
{
    $complete = (array)$_POST['iscomplete'];
    $uiuser = (array)$_POST['uidUser'];
    $connectId = (array)$_POST['connectID'];
    echo $complete;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.