getParams() 与 JsonArrayRequest

我试图根据我发送的数据从服务器获取响应,我能够从服务器获得响应,但服务器似乎没有接收到 getParams() 中的值。


       String url = "myUrl";

        RequestQueue queue = Volley.newRequestQueue(getContext());

        schoolContributeList = new ArrayList<>();

        JsonArrayRequest jsonRequest = new JsonArrayRequest(Request.Method.POST, url,

                new Response.Listener<JSONArray>() {

                    @Override

                    public void onResponse(JSONArray ja) {

                        try {

                            for (int i = 0;i < ja.length();i++) {

                                JSONObject school = ja.getJSONObject(i);


                                String schoolId = school.getString("id");

                                String schoolName = school.getString("name");

                                universityItem schoolItem = new universityItem(schoolName, schoolId);


                                Toast.makeText(getContext(),schoolId , Toast.LENGTH_SHORT).show();

                                schoolContributeList.add(schoolItem);

                                schoolContributeAdapter =  new universityAdapter(getContext(), schoolContributeList);

                                schoolContributeSpinner.setAdapter(schoolContributeAdapter);

                            }


                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }

                }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) 

            {

                Log.i("error", error.toString());

                Toast.makeText(getContext(), "no response", Toast.LENGTH_SHORT).show();

            }

        })


白衣染霜花
浏览 149回答 1
1回答

温温酱

在这种情况下,我建议的方法是在代码的 URL 中传递参数并删除 getParams() 方法。因此,您修改后的代码如下所示;&nbsp;String url = "myUrl";&nbsp; &nbsp; RequestQueue queue = Volley.newRequestQueue(getContext());&nbsp; &nbsp; schoolContributeList = new ArrayList<>();&nbsp; &nbsp; JsonArrayRequest jsonRequest = new JsonArrayRequest(Request.Method.POST, url+"?schoolUniversity="+selectedUniversityItem.getmUniversityValue()+"&key="+key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<JSONArray>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONArray ja) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0;i < ja.length();i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject school = ja.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String schoolId = school.getString("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String schoolName = school.getString("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; universityItem schoolItem = new universityItem(schoolName, schoolId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getContext(),schoolId , Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; schoolContributeList.add(schoolItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; schoolContributeAdapter =&nbsp; new universityAdapter(getContext(), schoolContributeList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; schoolContributeSpinner.setAdapter(schoolContributeAdapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("error", error.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getContext(), "no response", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }){&nbsp; &nbsp; };&nbsp; &nbsp; queue.add(jsonRequest);希望这会奏效。祝你好运 :)
打开App,查看更多内容
随时随地看视频慕课网APP