What is the difference between JSONRequest and StringRequest in volley
Asked Answered
S

2

7

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!

Serenity answered 7/8, 2017 at 9:26 Comment(5)
#32420658Rhinelandpalatinate
unfortunately It didn't helpSerenity
JSONRequest returns the result as a JSONObject. The StringRequest returns the result as a String(the json structure on a string)Abele
so whole json response is returned in string format?Serenity
If you expect the response to be a JSON object or a JSON Array, save time from implementing serialization and you don't want to explicitly set the content type then JsonObjectRequest and JsonArrayRequest is the right tool you need. However if it is a different format or you just wanted to use some other serialization/deserialization tool like GSON then the StringRequest is the general tool for that. Well so far Volley is lacking of ByteRequest.Sovereign
R
8

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) {

                }
            });
Rhinelandpalatinate answered 7/8, 2017 at 9:50 Comment(9)
what is the benefit using string request, because data returning is still in json format and we have to parse to get string, why specific string requestSerenity
by this "StringRequest class will be used to fetch any kind of string data" did you mean if there is data on server that has for example numbers, url etc but string request will only return string in that data?Serenity
@Serenity Any doubt yet ??Rhinelandpalatinate
@Serenity for your first question read https://mcmap.net/q/1621052/-jsonrequest-vs-stringrequest-in-android-volleyRhinelandpalatinate
No, its intersting @IntelliJSerenity
So its for fetching string data only, from server?Serenity
@Serenity yes . Data is RAW (May be)Rhinelandpalatinate
What do you mean by raw?Serenity
@Serenity Raw data is data that has not been processed for useRhinelandpalatinate
A
1

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 ({).

Antepast answered 7/8, 2017 at 9:32 Comment(9)
It means response is still in json but in string request only format is changed? its like middleware which converts json into string format?Serenity
nope the response for StringRequest is String, the server returns String, and for the JSONObject the server returns a JSON responseAntepast
So that means server can also return data in string format along with json object?Serenity
I just knew that server returns data only in json formatSerenity
only one type of data can be returned from server (Json, xml, String ...)Antepast
So its only for fetching string data(on server) in json format?Serenity
it doesn't convert any thing to any format it just get it like it returnedAntepast
Only strin data on server?Serenity
that it like that.Antepast

© 2022 - 2024 — McMap. All rights reserved.