【问题标题】:My JsonObjectRequest POST Request not working?我的 JsonObjectRequest POST 请求不起作用?
【发布时间】:2021-08-31 13:19:13
【问题描述】:

我正在尝试使用 MySQL 数据库中的数据填充 Spinner。

public void getData() {
    /*To add parameters & values for the request*/
    Map < String, String > params = new HashMap < > ();
    params.put("Email", sessionemail);

    JSONObject parameters = new JSONObject(params);

    /*Create ArrayList to store vehicle_id to later get vehicle data from DB Query*/
    ArrayList < String > vehicleid = new ArrayList < > ();
    ArrayList < String > tagList = new ArrayList < > ();

    /*Initialize Json Object Request*/
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, parameters, response - > {
        try {
            //Parsing the fetched Json String to JSON Object
            JSONArray jsonArray = response.getJSONArray("tags");
            Log.d(String.valueOf(jsonArray), "JSON DATA");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String tag = jsonObject.optString("Tag");
                String vehicle = jsonObject.optString("Vehicle_Id");
                tagList.add(tag);
                vehicleid.add(vehicle);
                tagidAdapter = new ArrayAdapter < > (Alert.this, android.R.layout.simple_spinner_item, tagList);
                tagidAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                tagSpinner.setAdapter(tagidAdapter);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, Throwable::getStackTrace);

    /*Adding request to the queue*/
    requestQueue.add(jsonObjectRequest);
}

以下是我通过执行 GET 请求从我的 PHP 获得的数据:

{
    "tags": [{
        "Tag": "W1C6EZY583DB2907X4A",
        "Vehicle_Id": "4"
    }, {
        "Tag": "6QZL43YEWR6IMMFUING",
        "Vehicle_Id": "6"
    }, {
        "Tag": "STK6WQ8ONDOUMEFSGQF",
        "Vehicle_Id": "17"
    }, {
        "Tag": "93RPHOC8M2AAQ2P2TUX",
        "Vehicle_Id": "72"
    }, {
        "Tag": "UH31KR4F6EFGHJ33S82",
        "Vehicle_Id": "73"
    }]
}

所以基本上我需要将“Tag”值插入到 Spinner 中,但由于某种原因,当我到达 jsonObjectRequest 时,请求失败。

【问题讨论】:

  • 简化您的问题:首先,尝试不进行所有 JSON 处理:使用 应该 工作的数据对对象进行硬编码,然后尝试您的代码。它有效吗?很酷,那么问题不在于JsonObjectRequest,而在于您的有效负载创建代码。它不工作吗?很酷,这个问题与你的有效载荷创建工作无关,所以没有理由将它包含在你的问题中。为此,请始终记住尝试形成minimal reproducible example,而不是“为我们”,而是因为通过该练习几乎总是让您自己发现问题,如果没有,您有集中的代码要询问。跨度>
  • 请分享你的清单!
  • 我不知道这与您的问题有多相关,但我确实注意到您在使用 Log.d 时出现了错误。标签需要在方法调用中出现在第一位,即Log.d(tag, message)。我好像记得标签上有字符限制,这可能意味着这个方法会抛出错误。

标签: java json android-studio android-volley


【解决方案1】:

更改为 StringRequest,这解决了我的问题。

    StringRequest strRequest = new StringRequest(Request.Method.POST,
            URL,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String tag = jsonObject.optString("Tag");
                            String vehicle = jsonObject.optString("Vehicle_Id");
                            tagList.add(tag);
                            vehicleid.add(vehicle);
                            tagidAdapter = new ArrayAdapter<>(Alert.this, android.R.layout.simple_spinner_item, tagList);
                            tagidAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            tagSpinner.setAdapter(tagidAdapter);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            },

【讨论】:

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