2

I have a web service which gives data in JSON array format. Right now the data is being fetched just by passing the URL. But now I want to pass a parameter to get the JSON response. This web service has GET & POST methods. I tried with Volley - Sending a POST request using JSONArrayRequest answer, but I couldn't implement it in my code. It would be really helpful if somebody could explain, how to achieve this in my code.

This is how my code looks like

String HTTP_SERVER_URL = "https://192.168.1.7/STUDENTWS/Default.asmx/StudentDataJson?InFacultyID=string";
public void JSON_WEB_CALL(){

    //mSwipeRefreshLayout.setRefreshing(true);

    jsonArrayRequest = new JsonArrayRequest(HTTP_SERVER_URL,



            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsonArrayRequest);
}



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("STUDENTID"));

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

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


        }
        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);
    }
}
  • See if [this](https://stackoverflow.com/questions/33014210/how-to-post-request-parameters-when-using-jsonarrayrequest-in-volley) helps you. – Sandeep Singh Feb 19 '18 at 10:43

3 Answers3

2

For get with params you should create StringRequest. For example:

RequestQueue queue = Volley.newRequestQueue(context);

StringRequest sr = new StringRequest(Request.Method.GET, "http://headers.jsontest.com/",
  new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
      Log.e("HttpClient", "success! response: " + response.toString());
    }
  },
  new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
      Log.e("HttpClient", "error: " + error.toString());
    }
  })
  {
    @Override
    protected Map<String,String> getParams(){
      Map<String,String> params = new HashMap<String, String>();
      params.put("user","YOUR USERNAME");
      params.put("pass","YOUR PASSWORD");
      return params;
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String,String> params = new HashMap<String, String>();
      params.put("Content-Type","application/x-www-form-urlencoded");
      return params;
    }
  };
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
1
// for json object
Map<String, String> jsonParams = new HashMap<String, String>();
        jsonParams.put("fullname",fullName);
        jsonParams.put("email",email);
        jsonParams.put("password",password);

// for json array make any list.List will be converted to jsonArray
//     example-----
//     ArrayList<String> jsonParams =new ArrayList();
//     jsonParams.add("Test1")
//     jsonParams.add("Test2")

      JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "onErrorResponse: ",error );
                Toast.makeText(ActivitySignUp.this,error.getMessage(), Toast.LENGTH_LONG).show();

            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> header=new HashMap<String, String>();
                header.put("Content-Type","application/json");
                return header;
            }
        };
0

If you want to pass a param to your server, then you might wanna do a POST method, else.. if you only wanted to get the data, then GET method is the best way... there's also header that you can use to pass data, but, I wouldn't recommend to use header for sending params... that's a sore eyes

Edit


//Set you method POST or GET

JsonObjectRequest(Request.Method.GET(/*here can set to POST too*/), url, null,
    new Response.Listener<JSONObject>() 
    {
        @Override
        public void onResponse(JSONObject response) {   
                        // display response     
            Log.d("Response", response.toString());
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {            
            Log.d("Error.Response", response);
       }
    }
);
yuzuriha
  • 465
  • 1
  • 8
  • 18