Yoo
Yoo

Reputation: 1441

How to write PHP to receive JSON from Android

How to write PHP (server side) to receive JSON then insert into DB using Android.

I wrote Java to send data to the PHP server :

    HttpPost httpPost = new HttpPost("http://sit-edu4.sit.kmutt.ac.th/csc498/51270372/[My Folder].[My filename].php");

But I do not know how to write the PHP file to receive JSON then insert to DB.

Could you please help me ? Thank you.

Upvotes: 0

Views: 1649

Answers (1)

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5616

You have your JSON data somewhere in $_POST array.

You can decode the JSON like this:

$json = $_POST['data']; // replace 'data' with your key
$decoded = json_decode($json, TRUE);
if ($decoded === FALSE) {
  throw new Exception('Bad JSON format.');
}
// save to your database via PDO

Upvotes: 3

Related Questions