Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnru Sharma
287 PointsRegarding response
response.body() what does it return and what is the difference between string and toString() method
1 Answer
Nasir Mahamoud
22,892 Pointsresponse.body() returns a ResponseBody Object. Looking at the OkHttp Source Code, it looks like ResponseBody is an abstraction (representation) of a raw Http Response. Here is a short description of the ResponseBody class from the OkHttp Source Code:
"A one-shot stream from the origin server to the client application with the raw bytes of the response body."
string() and toString() are two different methods, from two different sources, that function differently.
string() is a method from the ResponseBody class. According to OkHttp:
"string returns the response as a string decoded with the charset of the Content-Type header".
toString() is a method from the Object class (which all classes inherit from) and returns a string representation of the object on which it is called. According to the Java Docs:
"The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object."
Typically a class will override toString() to provide a better string representation of it's objects. However, from what I can see, ResponseBody does not provide it's own implementation of toString().