失败的jsonArrayRequest

时间:2018-08-13 20:49:05

标签: arrays android-studio android-volley

要创建jsonArrayRequest的MY函数的内容为:

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
            FilesUsed.url_display_thought, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        JSONObject jsonObject1 = response.getJSONObject(0);
                        JSONObject jsonObject2 = response.getJSONObject(1);
                        if (jsonObject1.getBoolean("error")) {
                            ArrayList<String> list = new ArrayList<>();
                            list.add(jsonObject2.getString("message"));
                            displayThought(list);
                        } else {
                            int count = 0;
                            ArrayList<String> list = new ArrayList<>();
                            while (count < response.length()) {
                                try {
                                    JSONObject jsonObject = response.getJSONObject(count);
                                    String thought = jsonObject.getString("post");
                                    list.add(thought);
                                    count++;
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            displayThought(list);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            error.printStackTrace();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("email", "sainiakshay04");
            return params;
        }


    };

    RequestHandler.getInstance(this).addToRequestQueue(jsonArrayRequest);

它使用的php的内容是:

get_info_added(“ shared_content”);             if($ res == 1){             $ response = $ db-> display_shared_content();}             其他{                 $ response [] ['error'] = true;             $ response [] ['message'] =“没有要显示的内容”;}         }         其他{             $ response [] ['error'] = true;             $ response [] ['message'] =“错误:请求无效”;         }     回声json_encode($ response); ?>

但是它显示的输出是ERROR:INVALID REQUEST,尽管我使用getParams绑定电子邮件,但PHP的isset无法正常工作,并且将转到其他部分。 帮助!

1 个答案:

答案 0 :(得分:0)

您正在检查isset($_POST['email']),但在JsonArrayRequest中发送了null参数。

目前,JsonArrayRequest仅支持JsonArray参数。

您需要做两件事:

  1. 在JsonArray参数中发送电子邮件

    JSONObject request = new JSONObject();
    request.put("email", userEmail);
    
    JSONArray arrayParam = new JSONArray();
    arrayParam.put(request);
    
    ....
    
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, 
        FilesUsed.url_display_thought, arrayParam, ...
    
  2. 通过PHP获取电子邮件

    require_once '../app/display_personal_account.php';
    
    $response=array();
    
    $inputJSON = file_get_contents('php://input');
    $input = json_decode($inputJSON, TRUE);
    
    if(isset($input[0]['email'])){
        $db = new display_personal_account($input[0]['email']); 
    
        ....
    

之后,您应该可以正常工作:)