I try send and receive JSONObject on PHP site server. I use below sample code.
GlobalConfig config = new GlobalConfig();
JSONObject json = config.getConfig();
JSONObject data = new JSONObject();
try {
data.put("ID", json.getString("ID"));
data.put("session", json.getString("session"));
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, PROT + "://" + REMOTE + "/" + DIR + "/test.php", data,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// response
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
}
}
);
Below is my PHP script:
<?php
$json = json_decode($_POST['json']);
$date = date("Y-m-d H:i:s");
$obj = new stdClass();
$obj->id = $json->ID;
$obj->session = $json->session;
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "login: ".$obj->id.", ".session: ".$obj->session;
fwrite($myfile, $txt);
fclose($myfile);
?>
When I run my app, the file is created but only with the words login and session without variables. What is wrong?