如何在Retrofit 2中处理POST请求响应?
我试图将appVersion
和deviceName
作为String
发送到服务器,并获取systemDate
和token
为字符串作为响应。
这是我在Kotlin的POJO:
class InitPost { @SerializedName("appVersion") @Expose var versionName: String? = null @SerializedName("deviceName") @Expose var manufacturer: String? = null }
Api服务:
@POST("getRequest") Call postInit(@Body InitPost initPost);
在我的活动中:
apiService.postInit(initPost) .enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if(response.isSuccessful()){ Timber.d("INIT POST SUCCESSFUL"); } } @Override public void onFailure(Call call, Throwable t) { Timber.e("INIT POST UNSUCCESSFUL %s", t.getMessage()); } });
这是完美的,直到这一部分,我所能做的就是显示response.body().toString()
,我怎样才能得到字符串systemDate和字符串令牌作为响应,并将其保存在我的应用程序? 我用POSTMAN试了一下,回复没有问题,我只是不知道该怎么弄
你的回应POJO应该包含你所期待的所有领域
class InitPost { @SerializedName("appVersion") @Expose var versionName: String? = null @SerializedName("deviceName") @Expose var manufacturer: String? = null @SerializedName("systemDate") @Expose var systemDate: String? = null @SerializedName("token") @Expose var token: String? = null }
创建一个包含字段的DTO。
@SerializedName("systemDate") @Expose val systemDate: String? = null @SerializedName("token") @Expose val token: String? = null
然后将您的Call
通用types从Call
更改为Call<
NewPOJO >
。 在你的onResponse
回调中使用response.body()
,你将得到NewPOJO
对象
- compileReleaseKotlin失败,java.lang.ClassNotFoundException:com.sun.tools.javac.util.Context
- Android Kotlin ImageView绑定
- 将多个自定义LinearLayouts与按钮添加到ListView项目
- Kotlin – 不能使用'T'作为实体类型参数。 改用班级
- Android Kotlin Realm正确的查询方式+返回Bg主题上的非托管项目
- 如何正确显示Google房间使用的异步任务的进度
- 在Android 4.4上添加Kotlin配置后,DexFile不包含生成的类
- 当在kotlin表达不按预期工作
- 在“LatLngBounds”构建器上设置最大缩放级别