0

我在使用 Volley 时遇到问题。想要使用带有一些参数的 POST 方法并获得数组类型的响应,但我的响应不是数组类型。在这里,我分享我的请求代码和响应。

RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest postRequest = new StringRequest(Request.Method.POST, "https://umrahtech.com/umrahtechapi.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // response
                        Log.d("Response", response);
                        route = null;
                        route_spinner.setSelection(0);
                        check_in_date = null;
                        check_out_date = null;
                        adults = child = room = child1 = child2 = child3 = child4 = child5 = 0;
                        text_adults.setText("0 Adult");
                        text_child.setText("0 Child");
                        text_room.setText("0 Room");
                        layout_child.setVisibility(View.GONE);
                        in_date.setText("Add Date");
                        out_date.setText("Add Date");
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        Log.d("Error.Response", error.toString());
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("case", "hotel_makkah");
                params.put("location", route);
                params.put("check_in_1", check_in_date);
                params.put("check_out_1", check_out_date);
                params.put("passengers", room_array.toString());
                return params;
            }
        };
        queue.add(postRequest);
4

2 回答 2

0

当你使用字符串请求响应时,你得到的也将是字符串。您应该将该响应转为 JsonArray ,然后从该 JsonArray 中获取对象,如下所示:

if (response != null) {
                JSONArray fetchlist = JSONArray(response);
                for (int i=0 ; i<fetchlist .lenght ; i++) {
                    JSONObject obj =  fetchlist.getJSONObject(i);

Int idd = obj.getInt("genderid");

于 2021-02-24T11:21:03.520 回答
0

我以这种方式解决了这个问题。其中 hudx_Object 和 hudx_JSON 是 JSONObject

RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest hudxconnect = new StringRequest(Request.Method.POST, "https://umrahtech.com/umrahtechapi.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        hudx_Object = new JSONObject(response);
                        if (hudx_Object != null) {
                            hudx_JSON = hudx_Object.getJSONObject("response");
                            hudx_Object = new JSONObject(hudx_JSON.toString());
                        } else {
                            hudx_Object = null;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.toString());
                    progressDialog.dismiss();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("case", "hotel_makkah");
            params.put("location", route);
            params.put("check_in_1", check_in_date);
            params.put("check_out_1", check_out_date);
            params.put("passengers", room_array.toString());
            return params;
        }
    };
于 2021-03-03T10:42:16.880 回答