Reputation: 6307
I have always read that data comes from server in json format also when we want to send some data we send to the server in json format so data travels in json format then where string request comes from? I dont know if we can post and get data in string format also, and what is the difference and use cases for using string and json requests?
Thanks!
Upvotes: 7
Views: 11777
Reputation: 75788
StringRequest class will be used to fetch any kind of string data. The response can be json, xml, html,text.
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
If you are expecting json object in the response, you should use JsonObjectRequest .
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Upvotes: 8
Reputation: 5339
It's about the return type from the request, StringRequest
handles a String
as response like error = false
and JSONObjectRequest
handles a JSONObject
response like {"error" : false}
, how to know it's JSONObject
? with the usage of brackets ({
).
Upvotes: 1