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
4741 gold badge4 silver badges12 bronze badges
asked Feb 1, 2015 at 17:57
1

1 Answer 1

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));
 }
 }
}
answered Mar 11, 2015 at 7:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.