Meaning of“A type-safe HTTP client for Android and Java” in Retrofit 2.0

前端 未结 2 1460
别那么骄傲
别那么骄傲 2021-01-12 04:59

Can someone please explain meaning of Retrofit\'s tagline :

A type-safe HTTP client for Android and Java

相关标签:
2条回答
  • 2021-01-12 05:35

    Example: A call is made to a API endpoint to return all books by authors.

    1.GET /articles?include=author HTTP/1.1

    Response:

    HTTP/1.1 200 OK Content-Type: application/vnd.api+json { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!", "body": "The shortest article. Ever.", "created": "2015-05-22T14:56:29.000Z", "updated": "2015-05-22T14:56:28.000Z" }, "relationships": { "author": { "data": {"id": "42", "type": "people"} } } }], "included": [ { "type": "people", "id": "42", "attributes": { "name": "John", "age": 80, "gender": "male" } } ] }

    As you can see, the response has some data in the form of keys and values.

    Java language usually has built in libraries to parse such information. But, here is where Retrofit makes it much more easy.

    Retrofit is type-safe. Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.

    0 讨论(0)
  • 2021-01-12 05:47

    Type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types for the program's constants, variables, and methods (functions), e.g., treating an integer (int) as a floating-point number (float). This is common in statically typed languages such as Java and C

    Thus Retrofit prevents errors of this type

    0 讨论(0)
提交回复
热议问题