Prakash Raman
Prakash Raman

Reputation: 13943

Should I use StringRequest or JsonObjectRequest

I am a little confused as to which I am supposed to use.

The API is an trying to hit accepts regular text post parameters although its response is a JSON String.

Which should I use ?

Upvotes: 2

Views: 2506

Answers (3)

Kiran Kumar S
Kiran Kumar S

Reputation: 11

StringRequest - when you want to send api parameter (getParams()) and fetch json response, you can use StringRequest.

JsonObjectRequest - When you only want to fetch the json response and not pass an api parameter, you can use JsonObjectRequest. JsonObjectRequest can override the same set of methods (getHeaders(), getBodyContentType(), getBody(), getMethod()) like the StringRequest except getParams().

Upvotes: 0

Andro Chen
Andro Chen

Reputation: 108

JsonObjectRequest and StringRequest is different in their parent class and their response. You could find it out if you dig into volley's source code.

JsonObjectRequest extends JsonRequest<JSONObject>

StringRequest extends Request<String>

So, if the response is a JSON String, then you could just use JsonObjectRequest for convenience, since Volley has wrapped the response to be a JSONObject.

Upvotes: 2

Sandeep Singh
Sandeep Singh

Reputation: 1127

You Can use JSON Object Request for this.

Upvotes: 2

Related Questions