我正在尝试使用JSONObjectRequest
发布数据,但是它不起作用。它发布空值。我怎么解决这个问题 ?这是我的代码。
public void getData(){
String url = "http://address.com";
Map<String, String> params = new HashMap<>();
params.put("name","new");
params.put("number","number");
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String name_server = response.getString("name");
String number_server = response.getString("number");
textView.setText(name_server);
number.setText(number_server);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestque(jsonObjectRequest);
}
这是我的PHP代码
<?php
$name = $_POST["name"];
$number = $_POST["number"];
echo json_encode(array("name"=>"$name","number"=>"$number"));
?>
但请不要帮助我。