1

Im trying to make a post request with volley.

The request parameter is a json array .

Following is my request parameter

[
 "Type",
 {
  "User": "email",
  "password": "dimmer",

 }
]

I have a method frameJsonArray that frames the above json and im making post request as follows,

 JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();


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

                }
            }
    );

Im getting error in the above line of code where im making the request. How can I get this sorted?

Following is my error log

Error:(162, 46) error: constructor JsonArrayRequest in class JsonArrayRequest cannot be applied to given types;
required: String,Listener<JSONArray>,ErrorListener
found: int,String,JSONArray,<anonymous Listener<JSONObject>>,<anonymous ErrorListener>
reason: actual and formal argument lists differ in length

Following is my frameJsonArrayMethod

public JSONArray frameJsonArray() {
    JSONObject jsonObject = new JSONObject();
try {
    jsonObject.put("login_type", "Android");
    jsonObject.put("username", email);
    jsonObject.put("password", password);
    jsonObject.put("short_name", null);
    jsonObject.put("ip","123.421.12.21");

} catch (JSONException e) {
    e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put("Login");
jsonArray.put(jsonObject);

Log.d(TAG, "he;ll " + jsonArray.toString());
Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
return jsonArray;

}

3 Answers3

1
JsonArrayRequest req = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                     Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();            
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        pDialog.hide();
                    }
                });

Source : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

Raphael Teyssandier
  • 1,722
  • 2
  • 13
  • 25
  • yeah but How can I send my request parameter using the above code snippet? –  Mar 25 '16 at 11:32
  • http://stackoverflow.com/a/18052417/4854450, but for request to API, it's better to use Retrofit with GSON, it's faster than Volley and ASyncTask. – Raphael Teyssandier Mar 25 '16 at 11:38
0
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                    Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();


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

            }
        }
);

Change JSONObject to JSONArray in onResponse()

EDIT The error coming because there is only one constructor in JsonArrayRequest i.e.

public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener){}

source https://android.googlesource.com/platform/frameworks/volley/+/e7cdf98078bc94a2e430d9edef7e9b01250765ac/src/com/android/volley/toolbox/JsonArrayRequest.java

you are using some constructor with 5 arguments.

Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
0

once check your JSON , wrong format , you missed double quote.

[
 "Type",
 {
  "User": "email /** you missed double quote here **/,
  "password": "dimmer",

 }
]
shobhan
  • 1,460
  • 2
  • 14
  • 28
  • i have corrected it, the error is upon making a json array request itself –  Mar 25 '16 at 11:31
  • the actual error is upon making jsonarrayrequest which is unresolved yet –  Mar 25 '16 at 11:34