I have created JSONRequest by using volley, It successfully hits the service, I checked the service end, It receives the data, and send "Success" in return.
The problem is that, Service returns String
in output, and Volley excepts some JSON Data
in output. So it executes the onError
Method, instead of onResponse
.
Kindly guide me how to make it accept string response, or is it not possible when you are using JSONObject
as request.
Request<JSONObject> jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.101:8888/api/services/mytest",
jsonParent, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Success", response.toString());
deleteFile();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.toString());
deleteFile();
}
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
Use newRequestQueue RequestQueue queue = Volley. newRequestQueue(this); String url = "https://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request. Method.
JsonObjectRequest : A request for retrieving a JSONObject response body at a given URL, allowing for an optional JSONObject to be passed in as part of the request body.
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.
You can use StringRequest instead JSONRequest.
StringRequest stringRequest = new StringRequest(methodType, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers == null ? super.getHeaders() : headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
return "Your JSON body".toString().getBytes();
}
};
getheaders
method is to add custom headers if you want and getBody
to supply request body
.
For those who still need to send raw Json and receive a String as response:
By default, the body consists of the request parameters in application/x-www-form-urlencoded; charset=UTF-8
format for StringRequest
. When overriding getBody()
, as Nayan suggested correctly, consider overriding getBodyContentType()
as well to match the new body format. For Json it should be
application/json
.
String url = "www.google.com";
HashMap<String, Object> params = new HashMap<>();
params.put("key1", "Android");
params.put("isCool", true);
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
}
}
) {
public byte[] getBody() {
return new JSONObject(params).toString().getBytes();
}
public String getBodyContentType() {
return "application/json";
}
};
Volley.newRequestQueue(this).add(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With