我尝试在PHP站点服务器上发送和接收JSONObject。我使用下面的示例代码。
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
}
}
);
以下是我的PHP脚本:
<?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);
?>
当我运行我的应用程序时,文件已创建,但只有单词login和session without variable。有什么问题?