【问题标题】:$_POST in php not populated using Volley library JsonObjectRequest (method POST)php 中的 $_POST 未使用 Volley 库 JsonObjectRequest 填充(方法 POST)
【发布时间】:2018-02-02 13:35:55
【问题描述】:

让我们从我正在使用的代码开始,我尝试了每一种可能的不同方式来制作“参数”。我将它用作 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);
?>

【问题讨论】:

  • 如果要使用 $_ POST 数组,请不要发送 json 参数。
  • @Pavneet_Singh 我已经阅读了该问题,但由于我使用的是 android 应用程序并且我的目标是发送数据以填充数据库,因此我不能使用从文件中读取的方法。我缺乏知识是一个巨大的耻辱,你在正确的地方,谢谢,这就是解决方案。

标签: php android json android-volley


【解决方案1】:

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);
?>

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 1970-01-01
    • 2013-11-19
    • 2015-08-10
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多