1

I am sending through some values from android/java to a server.

First I create a JSONArray object:

JSONArray JSONsamples = new JSONArray();

then I populate the JSONArray the following way:

 for(.....)
                {
                    if(...)
                    {
                      JSONsamples.put(storedArraylist.get(i));
                    }
                }

Then I send the value through post :

userParameters.add(new BasicNameValuePair("samples", JSONsamples.toString()));
.
.
Http_Client.executeHttpPost(getApplicationContext(), POST_url, userParameters);
.

Then on the server I try to store those values into a php array, but it does not work:

 $samplesarray = $_POST["samples"];
 $samplesarray = json_decode($samplesarray,true);

Before decoding, If I echo $samplesarray , I only get the first value encapsulated in these \"value\" , after json_decode , when I echo $samplesarray I get a parameter expected but null found error.

Please help.

16
  • 2
    echo $samplesarray to see exactly what do you get and let us know Commented Sep 12, 2013 at 11:00
  • 1
    it seems that \"blood\" is what JSONsamples.toString() returns. Or maybe it is "blood" if you have enabled magic_quotes. What is the expected return value of JSONsamples.toString()? Commented Sep 12, 2013 at 11:08
  • 1
    what is the output of JSONSamples.toString() in your ANdroid code Commented Sep 12, 2013 at 11:12
  • 1
    The \"blood\" is not a valid JSON thats the problem I think. Commented Sep 12, 2013 at 11:13
  • 2
    ignore the resulting []! it happens because you cannot json_decode a string like \"blood\". JSONsamples.toString() should return something like this: ["value1","value2","value3"] or {"key1":"value1","key2":"value2"} and you need to disable magic_quotes! Commented Sep 12, 2013 at 11:15

2 Answers 2

1

Magic Quotes should be turned off because it adds a \ before your " values. This breakes the json data in your case.

Please read more about why you should not use magicquotes here: http://php.net/manual/en/security.magicquotes.whynot.php

Sign up to request clarification or add additional context in comments.

Comments

0

The solution is removing magic quotes, or at least checking if its on or not and working from there. @steven reminded me of this, thank you.

So on the server side, I do the following check to ensure correct json and it works :

$samplesarray = get_magic_quotes_gpc() ? stripslashes($_REQUEST['samples']) : $_REQUEST['samples'];

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.