This article is for developers who want to query a POST request to server and parse the response into a custom object in Android Kotlin using Retrofit library in a simplest way.
本文适用于想要查询对服务器的POST请求并以最简单的方式使用Retrofit库将响应解析为Android Kotlin中的自定义对象的开发人员。
Gradle依赖 (Gradle dependency)
You need to add below dependencies into your Android projects build.gradle (Module: app) file
您需要将以下依赖项添加到Android项目的build.gradle (模块:app)文件中
API和原始JSON请求响应 (API and raw JSON Request Response)
Now let’s assume our server endpoint for your REST request is http://api.server.com/users and we want to add a new user in server using http POST method call. Server spec for request and response might look like,
现在,假设您的REST请求的服务器端点是http://api.server.com/users ,我们想使用http POST方法调用在服务器中添加新用户。 请求和响应的服务器规范可能如下所示:
Request body json:
请求正文json:
Response body json:
响应正文json:
We see, when we hit the endpoint with above request body, server inserts the user info into its database, creates a new user ID for that particular user and returns the same user including the assigned new user ID. We want to parse the response user into the client apps custom object.
我们看到,当我们使用上面的请求正文敲击端点时,服务器将用户信息插入其数据库,为该特定用户创建一个新的用户ID ,并返回同一用户,包括分配的新用户ID。 我们想将响应用户解析为客户端应用程序自定义对象。
Kotlin数据模型类 (Kotlin Data Model Class)
We need to prepare model class UserInfo. As we see, json keys for request and response are not following coding convention of Android, so we need to add SerializedName annotation for keys so that when we use Retrofit it generates proper json with those keys. Using SerializedName we can map our property to jason key. Remember it is better to make the properties optional so that even if server does not response with all the keys we still able to create our rich object in client side.
我们需要准备模型类UserInfo 。 如我们所见,用于请求和响应的JSON密钥未遵循Android的编码约定,因此我们需要为密钥添加SerializedName批注,以便在使用Retrofit时会使用这些密钥生成正确的json。 使用SerializedName,我们可以将属性映射到jason key。 请记住,最好将属性设置为可选,这样即使服务器没有对所有键进行响应,我们仍然可以在客户端创建富对象。
改造工作流程 (Retrofit Workflow)
Now, we need three components to work with Retrofit REST calls,
现在,我们需要三个组件来处理Retrofit REST调用,
RestApi interface
RestApi界面
ServiceBuilder object
ServiceBuilder对象
RestApiService class
RestApiService类
RestApi interface
RestApi界面
In RestApi interface, we declare all the REST request methods with individual paths for the endpoint. Although example is shown for a typical POST call, we can use any REST methods on our own i.e. POST, GET, PUT, DELETE etc.
在RestApi接口中,我们使用端点的各个路径声明所有REST请求方法。 尽管显示了一个典型的POST调用示例,但是我们可以自己使用任何REST方法,即POST,GET,PUT,DELETE等。
2. ServiceBuilder object
2. ServiceBuilder对象
We create a builder of the retrofit object which can be reused for all method calls declared in the RestApi interface. We can set many properties like, converter factory for json parsing, base url, http client and many more configurations as required. Here is the simplest required form for our tasks.
我们创建了改造对象的构建器,该构建器可以在RestApi接口中声明的所有方法调用中重复使用。 我们可以根据需要设置许多属性,例如用于json解析的转换器工厂,基本url,http客户端以及许多其他配置。 这是我们任务最简单的必需表格。
3. RestApiService class
3. RestApiService类
Finally, we implement the actual Service which we will directly invoke. We call it service but don’t be confused with Android service. If you feel you can rename the class as your own like with RestApiManager or ApiManager etc.
最后,我们实现了将直接调用的实际服务。 我们称其为服务,但不要与Android服务混淆。 如果您觉得可以使用RestApiManager或ApiManager等将类重命名为您自己的类。
通话示例 (Example of calling)
Now we can call RestApiService’s method addUser() from any point of our application. A completion block onResult is used so that the result can be send back to the caller asynchronously. We see if the request succeeds we return the body of the response parsed into our custom object UserInfo to the callers and if the request fails, we return null.
现在,我们可以在应用程序的任何位置调用RestApiService的方法addUser ()。 使用完成块onResult可以将结果异步发送回调用方。 我们查看请求是否成功,将解析为自定义对象UserInfo的响应正文返回给调用方,如果请求失败,则返回null 。
So far this is the simplest workflow to handle a POST request to server and parse the response into custom rich object on Android Kotlin. The same workflow can be used for other REST methods with minimal changes.
到目前为止,这是处理对服务器的POST请求并将响应解析为Android Kotlin上的自定义富对象的最简单的工作流程。 只需进行最小的更改,即可将相同的工作流程用于其他REST方法。
翻译自: https://medium.com/swlh/simplest-post-request-on-android-kotlin-using-retrofit-e0a9db81f11a
所有评论(0)