I am new to Volley and i'm trying to make Post request work with
JsonArrayRequest
From many answers I've found on stackoverflow, I tried as follows -
private void getCounts() {
String url = "http://192.168.1.100/json/count.php";
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("LastUpdatedAt", "0");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
@Override
public byte[] getBody() {
Map<String, String> params = new HashMap<String, String>();
params.put("LastUpdatedAt", "0");
if (params != null && params.size() > 0) {
Log.d(TAG, encodeParameters(params, getParamsEncoding()).toString());
return encodeParameters(params, getParamsEncoding());
}
return null;
}
protected byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
};
AppController.getInstance().addToRequestQueue(req);
}
As one can see, I have tried both, getParams()
and getBody()
to send LastUpdatedAt
as Post request, but no matter what I try the value is not posted and returns null on server.
I even tried with using JsonArrayRequest(Request.Method.POST, ...
and also by sending 'params' as JsonObject in the request, but even these two didn't work.
In one of the similar question, use of following class is suggested -
public class CustomJsonRequest extends Request {
Map<String, String> params;
private Response.Listener listener;
public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
Response.Listener responseListener, Response.ErrorListener errorListener) {
super(requestMethod, url, errorListener);
this.params = params;
this.listener = responseListener;
}
@Override
protected void deliverResponse(Object response) {
listener.onResponse(response);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected Response 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));
}
}
}
But I couldn't make working Response listner createRequestSuccessListener
and its error listener.
In the same similar question, following constructor is suggested -
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
But, I could not figure out how to make it work?
答案 0 :(得分:2)
尝试做类似的事情:
String url = "http://192.168.1.100/json/count.php";
final JSONObject _jsonRequest = new JSONObject();
_jsonRequest.put("key", value);
JsonArrayRequest _stringRequest = new JsonArrayRequest(
url , new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Your code here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Your code here
}
}){
@Override
public byte[] getBody() {
try {
return _jsonRequest.toString().getBytes(getParamsEncoding());
} catch (UnsupportedEncodingException uee) {
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> _params = new HashMap<String, String>();
_params.put("Content-type", "application/json");
return _params;
}
};
AppController.getInstance().addToRequestQueue(_stringRequest);