Java 如何使用Volley库在PHP站点服务器上接收JSONObject?
我尝试在PHP站点服务器上发送和接收JSONObject。我使用下面的示例代码Java 如何使用Volley库在PHP站点服务器上接收JSONObject?,java,php,android,post,android-volley,Java,Php,Android,Post,Android Volley,我尝试在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("ses
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
}
}
);
GlobalConfig config=new GlobalConfig();
JSONObject json=config.getConfig();
JSONObject数据=新的JSONObject();
试一试{
data.put(“ID”,json.getString(“ID”);
data.put(“session”,json.getString(“session”);
}捕获(JSONException e){
e、 printStackTrace();
}
RequestQueue=Volley.newRequestQueue(this);
JsonObjectRequest postRequest=新的JsonObjectRequest(Request.Method.POST,PROT+“://“+REMOTE+”/“+DIR+”/test.php),数据,
新的Response.Listener()
{
@凌驾
公共void onResponse(JSONObject响应){
//回应
}
},
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误){
//错误
}
}
);
下面是我的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);
?>