php 中的 $_POST 未使用 Volley 库 JsonObjectRequest(方法 POST)填充



让我们从我正在使用的代码开始,我已经尝试了所有可能的不同方法来制作"参数"。我已将其用作HashMap,Json格式和字符串。我还尝试通过创建哈希映射并返回它来@Override getParams(( 方法。没有任何工作。

这是我调用 JsonObjectRequest 的函数。

private void sendJsonObjReq() {
showProgressDialog();
Map<String, String> para = new HashMap<>();
para.put("Condicao", "dea");
para.put("Field", "1550");
JSONObject jason = new JSONObject(para);
String params = jason.toString();
textView.setText(params);
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(AppController.TAG, response.toString());
textView.setText(response.toString());
/*try {
int u = response.getInt("sucess");
if(u == 1){
JSONArray json = response.optJSONArray("Type");
if(json != null) {
MakeListHashMap(json);
}else{
textView.setText("Wrong Parameters");
}
}else{textView.setText("success is 0");}
}catch(JSONException e){
Log.e("Error:", e.getMessage());
textView.setText("nothing here");
}*/
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(AppController.TAG, "Error:" + error.getMessage());
showProgressDialog();
}
});
//Add to request queue
AppController.getInstance().addToRequestQueue(jsonReq, tag_json_obj);
}

url 和其他一切都很好,我已经检查过了,但我只是不明白为什么 GET 或 POST 方法都不起作用,因为我已经尝试了这两种方法,但我没有任何成功。我的 php 代码包括以下内容:

<?php
$response = array();
//$oi = json_decode($_POST);
//$response["Condicao"] = $_GET["Condicao"];
//$response["Field"] = $_GET["Field"];
$response["Condicao"] = $_POST["Condicao"];
$response["Field"] = $_POST["Field"];
$response['sucess'] = 1;
$response['other'] = "test";
echo json_encode($response);
?>

我试过解码它而不解码它,我真的不知道该怎么办。我认为这可能是服务器的问题,但是使用应用程序"邮递员"我可以发送GET和POST,并且响应是我想要的Json数组。

如果我发送 key1= Condicao => name=dea; key2= 字段 => 名称 = 1550;

我得到结果

{
"Condicao": "dea",
"Field": "1550",
"sucess": 1,
"other": "test"
}

编辑:解决方案是:

<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$response = array();
$response["Condicao"] = $_POST["Condicao"];
$response["Field"] = $_POST["Field"];
$response['sucess'] = 1;
$response['other'] = "test";
echo json_encode($response);
?>

1.( 传递 json 对象而不是字符串

JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, jason ,
//          ^^^^
new Response.Listener<JSONObject>() {

2.( 在您的 php 中接收它作为

<?php
$response   = array();
// receive your json object
$jasonarray = json_decode(file_get_contents('php://input'),true);
$response["Condicao"] = $jasonarray["Condicao"];
$response["Field"]    = $jasonarray["Field"];
$response['sucess']   = 1;
$response['other']    = "test";
echo json_encode($response);
?>

最新更新