3

I tried this android code for send json objects to my website

HttpPost httppost = new HttpPost(url);

JSONObject j = new JSONObject();
    j.put("name","name ");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        String format = s.format(new Date()); 
    nameValuePairs.add(new BasicNameValuePair("msg",j.toString() ));

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                               httpclient.execute(httppost);

And this php code

<?php

$msg=$_POST["msg"]; 

$filename="androidmessages.html";

file_put_contents($filename,$msg."<br />",FILE_APPEND);

$androidmessages=file_get_contents($filename);

echo $androidmessages;
?>

It will show me {"name":"name "} but if i use

httppost.setHeader( "Content-Type", "application/json" );

it will show nothing.I have no before experince about json object post but i think something went wrong.I want to send some user information to my website and display it in web page can you please tell me what i need to change to overcome this problem Thank you

2 Answers 2

2

Finally i got the answer

$msg=json_decode(file_get_contents('php://input'), true); 

$filename="androidmessages_json.html";

file_put_contents($filename,$msg['name']."<br />",FILE_APPEND); 

$androidmessages=file_get_contents($filename);

echo $androidmessages;
Sign up to request clarification or add additional context in comments.

Comments

1

It depends how you want to pass the data to your PHP-script.

If you want to get the JSON as a string in one variable (and maybe others in addition), then you should not use the content-type "application/json". If you want to only post the JSON without any variable, you can do the following instead:

HttpPost httppost = new HttpPost(url);
JSONObject j = new JSONObject();
j.put("name","name ");
httppost.setEntity(new StringEntity(j.toString());
httpclient.execute(httppost);

As you can imagine from the code you do not have the JSON in one POST-var only but in total.

6 Comments

Thank you christian. and i want to know what are different between these 2 methods (String in one variable and Post json without variable)
Post without variable means you will not get it in your $_POST-array, but have to get it by json_decode(file_get_contents('php://input'), true);
$msg=json_decode(file_get_contents('php://input'), true); $filename="androidmessages.html"; file_put_contents($filename,$msg."<br />",FILE_APPEND); $androidmessages=file_get_contents($filename); echo $androidmessages; Is this correct?
why do you need the file? I would just say echo json_decode(file_get_contents('php://input'), true);
It will doesn't show anything but if i use file it will show "Array" but it also doesn't show my details in json object
|

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.