0

I am pretty new to android developing and working with volley and got a few questions. I am trying to send a JSONArray via Post with volley and try to get back a String as response.

I have downloaded the volley files via github and got within the toolbox the JsonArrayRequest.java. This file contains a lot of information which I need for my task so I decided to mix it with the StringRequest.java and got out following:

New class called SendingJsonArray.java

package com.android.volley.toolbox;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;

/**
 * A request for retrieving a {@link JSONArray} response body at a given URL.
 */
public class SendingJsonArray extends JsonRequest<JSONArray> {

    /**
     * Creates a new request.
     * @param url URL to fetch the JSON from
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public SendingJsonArray(String url, JSONArray JsonArry, Listener<JSONArray> listener, ErrorListener errorListener) {
        super(Method.POST, url, JsonArry, listener, errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}
  1. The first strange thing is that SendingJsonArray() doesn't contain a int method? It compiles and seems to work without it.

  2. Than I changed the Method.GET to Method.POST. It compiles and seems to work again.

  3. Than I added the "JSONArray JsonArry" within the SendingJsonArray and get following error:

    error: no suitable constructor found for JsonRequest(int,String,JSONArray,Listener<JSONArray>,ErrorListener)
    [javac]         super(Method.POST, url, JsonArry, listener, errorListener);
    [javac]         ^
    [javac]     constructor JsonRequest.JsonRequest(int,String,String,Listener<JSONArray>,ErrorListener) is not applicable
    [javac]       (actual argument JSONArray cannot be converted to String by method invocation conversion)
    [javac]     constructor JsonRequest.JsonRequest(String,String,Listener<JSONArray>,ErrorListener) is not applicable
    [javac]       (actual and formal argument lists differ in length)
    [javac] 1 error
    

OK I know what the problem is but I don t know how to solve this. Where is the original constructor and how can I modify mine that this will work?

  1. If I get this working there will be the response listener left. I would try to copy the listener of StringRequest.java and hope that this will work?

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }
    
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }
    

Why are there two and what exactly are they created for? Sorry but the documentation about that isn't really helpful for me :/

Thanks for your help! :)

Saurabh
  • 434
  • 1
  • 4
  • 12
user3325230
  • 507
  • 1
  • 6
  • 19
  • this might help with GET : http://stackoverflow.com/questions/29761634/volley-customer-request-do-not-take-params/29793139#29793139 and this might help with POST: http://stackoverflow.com/questions/29779447/put-arraylist-into-param-jsonobject/29791979#29791979 – TommySM Apr 22 '15 at 09:39

1 Answers1

1

In order to send a JSONArray as param with volley library I implemented this class called JSONPostArrayRequest, which extends JSONRequest, maybe it can help you:

public class JsonPostArrayRequest extends JsonRequest<JSONObject> {

    JSONArray params;
    public JsonPostArrayRequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener,JSONArray params) {
        super(Method.POST, url, null, listener, errorListener);
        this.params=params;
    }

     @Override
     public byte[] getBody()  {
            if ( this.params != null && this.params.length() > 0) {
                return encodeParameters( this.params, getParamsEncoding());
            }
            return null;

      }

     private byte[] encodeParameters(JSONArray params, String paramsEncoding) {
            try {
                return params.toString().getBytes(paramsEncoding);
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
            }
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString =
                        new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
}
Álvaro Pérez Soria
  • 1,443
  • 1
  • 13
  • 26