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));
}
}
}
The first strange thing is that
SendingJsonArray()
doesn't contain a int method? It compiles and seems to work without it.Than I changed the
Method.GET
toMethod.POST
. It compiles and seems to work again.Than I added the
"JSONArray JsonArry"
within theSendingJsonArray
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?
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! :)