[英]$_POST in php not populated using Volley library JsonObjectRequest (method POST)
Let's start with the code I am using, I have tried every single possible different way to make "params". 让我们从我正在使用的代码开始,我已经尝试了每种可能的不同方式来制作“params”。 I have used it as a HashMap, in Json format and also as a string. 我已经将它用作Jash格式的HashMap,也用作字符串。 I have also tried to @Override the getParams() method by creating a hashmap and returning it. 我还尝试通过创建一个hashmap并返回它来@Override getParams()方法。 Nothing as worked. 什么都没有。
Here is my function that calls the JsonObjectRequest. 这是我调用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);
}
The url and everything else is fine, i have checked but i just cannot understand why neither the GET or POST method work, since i have tried both but I did not have any success with them. 网址和其他一切都很好,我已经检查但我无法理解为什么GET或POST方法都不起作用,因为我已经尝试了两种方法,但我没有取得任何成功。 My php code consists of this: 我的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);
?>
I have tried decoding it and not decoding it, I really am at a loss for what to do. 我试过解码它而不解码它,我真的不知所措。 I think it might be a problem with the server but using the App "Postman" I can send GET and POST and the response is a Json array like i want. 我认为它可能是服务器的问题,但使用App“邮差”我可以发送GET和POST,响应是我想要的Json数组。
If i send key1= Condicao => name=dea; 如果我发送key1 = Condicao => name = dea; key2= Field => name=1550; key2 = Field => name = 1550;
I get the result 我得到了结果
{
"Condicao": "dea",
"Field": "1550",
"sucess": 1,
"other": "test"
}
edit: The solution is: 编辑:解决方案是:
<?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.) Pass your json object instead of string 1.)传递你的json对象而不是字符串
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, jason ,
// ^^^^
new Response.Listener<JSONObject>() {
2.) Receive it in your php as 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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.