如何在发送JSONArray请求时发送参数

时间:2018-02-20 07:31:22

标签: android android-volley

我必须访问一个Web服务,它接受一个参数并给出JSONArray作为响应。我想知道如何发送POST参数,以便我在JSONArray中得到响应。  到目前为止,我已正确完成了JSON解析部分,并且数据正在回收器视图中填充。我试图覆盖protected Map<String, String> getParams(),如一个教程中所示但该覆盖显示错误"Method does not override method from its superclass"。请查看我的代码并指导我如何正确实现它。

public void JSON_WEB_CALL(){

    JsonArrayRequest jsArrRequest = new JsonArrayRequest
            (Request.Method.GET, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    //mTxtDisplay.setText("Response: " + response.toString());
                    JSON_PARSE_DATA_AFTER_WEBCALL(response);

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }


                @Override //This Override is showing an error "Method does not override method from its superclass"
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("param1", "one");
                    params.put("param2", "two");
                    return params;
                }

            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsArrRequest);

}



public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

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

        DataModel GetDataModel = new DataModel();

        JSONObject json = null;
        try {
            json = array.getJSONObject(i);

            GetDataModel.setId(json.getString("PRODID"));

            GetDataModel.setPlateNo(json.getString("GRADE"));

            GetDataModel.setPlateCode(json.getString("COLOR"));


        }
        catch (JSONException e)
        {

            e.printStackTrace();
        }

        DataAdapterClassList.add(GetDataModel);
        mSwipeRefreshLayout.setRefreshing(false);

    }

    recyclerViewadapter = new NewRecyclerViewAdapter(DataAdapterClassList, this);

    recyclerView.setAdapter(recyclerViewadapter);

    if (array.length()!=0) {
        SHOW_ALERT(array);
        sendNotification(recyclerView, array);
    }
}

2 个答案:

答案 0 :(得分:1)

您正在调用POST请求,因此您使用Request.Method.POST代替Request.Method.GET 请使用以下代码

public void JSON_WEB_CALL(){

    JsonArrayRequest jsArrRequest = new JsonArrayRequest
            (Request.Method.POST, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    //mTxtDisplay.setText("Response: " + response.toString());
                    JSON_PARSE_DATA_AFTER_WEBCALL(response);

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }


                @Override //This Override is showing an error "Method does not override method from its superclass"
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("param1", "one");
                    params.put("param2", "two");
                    return params;
                }

            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsArrRequest);

}

答案 1 :(得分:0)

首先使用params使用键值对创建Hashmap并传递

    HashMap<String ,String> params=new HashMap<String, String>();
    params.put("key",value);

JsonArrayRequest jsArrRequest = new JsonArrayRequest
            (Request.Method.GET, HTTP_SERVER_URL, params, new Response.Listener<JSONArray>() {
  //....
}