我需要通过改造2发送下一个json:
{
"Inspection": {
"UUID": "name",
"ModifiedTime": "2016-03-09T01:13",
"CreatedTime": "2016-03-09T01:13",
"ReviewedWith": "name2",
"Type": 1,
"Project": {
"Id": 41
},
"ActionTypes": [1]
}
}
带标题:
Authorization: access_token_value
我试过这个:
//header parameter
String accessToken = Requests.getAccessToken();
JsonObject obj = new JsonObject();
JsonObject inspection = new JsonObject();
inspection.addProperty("UUID","name");
inspection.addProperty("ModifiedTime","2016-03-09T01:13");
inspection.addProperty("CreatedTime","2016-03-09T01:13");
inspection.addProperty("ReviewedWith","name2");
inspection.addProperty("Type","1");
JsonObject project = new JsonObject();
project.addProperty("Id", 41);
inspection.add("Project", project);
obj.add("Inspection", inspection);
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(Constants.ROOT_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
Call<JsonElement> result = service.addInspection(accessToken, obj);
JsonElement element = result.execute().body();
但是每次我收到异常:
java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)
如何发送?或者我有什么其他想法可以做到这一点。你甚至可以给我提供简单的参数
String
,里面有json。它会适合我
解决方案: 使用 next 在界面中声明 body 值:
@Body RequestBody body
并包装 String JSON 对象:
RequestBody body = RequestBody.create(MediaType.parse("application/json"), obj.toString());
您有可能在多个
@SerializedName("")
中保持相同的
vairable/fields/tags
您可以在创建 Retrofit 时指定一个 Converter,如下所示
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseurl)
.client(okHttpClient)
.build();
如果这是由于
@SerializedName
造成的,请确保它没有重复。
例如以下情况会抛出此错误:(注意:
bookingId
被传递了两次)
@SerializedName(value="bookingId", alternate={"id", "bookingId"})
但是,这是正确的:
@SerializedName(value="bookingId", alternate={"id", "someOtherId", "whateverId"})
Body
使用单个请求对象,声明您的请求对象如下
class Inspection {
String UUID;
//..... add your fields
Project project;
}
class Product
{
int Id;
//....... add your fields
}
我假设您的服务
IConstructSecureAPI
端点是:
@GET(...) // change based on your api GET/POST
Call<Response> addInspection(
@Header("Authorization") String accesstoken,
@Body Inspection request
);
你可以说出你的愿望
Response
。
检查这个答案,它使用
HashMap
而不是类。
当我升级到Java 17时,我不断收到此错误,但在Java 11上仍然工作正常。
Utils.java
中放置了一个调试点。java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte java.time.LocalTime.hour accessible: module java.base does not "opens java.time" to unnamed module @35e2d654
--add-opens java.base/java.time=ALL-UNNAMED
作为 JVM 参数。我正在使用
Moshi
和 Retrofit
,我的问题是我忘记为 @JsonSerializable
的 DTO
类添加 @body
注释。
你应该像这样添加这个注释:
@JsonSerializable
data class RegisterDTO(
@field:Json(name = "device_id") val deviceId: String,
)
就我而言,我只是忘记为 Gradle 应用 kotlinx.serialization 插件,因此没有生成序列化器的代码。通过以下方式修复:
plugins {
kotlin("plugin.serialization")
}
您可以使用拦截器在每个请求中发送授权标头
class AuthorizationInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
String authorizationToken = AuthenticationUtils.getToken();
Request authorizedRequest = originalRequest.newBuilder()
.header("Authorization", authorizationToken)
.build();
return chain.proceed(authorizedRequest);
}
}
我遇到了一个关于这个问题的非常奇怪的案例。因此,我有一个用于解析的模型,该模型的
List
对象字段具有错误的 SerializedName
值。以前还好,因为我没有用它来做任何事情。但现在我想用它来做点什么,所以我修复了它。然后我检查了 List
引用的模型,发现我也可以用它来解析 Retrofit
,即使它没有 SerializedName
,所以我为引用的每个字段添加了一个模型/对象。此后错误开始出现。我检查了我的 .addConverterFactory(GsonConverterFactory.create())
构建器上确实有 Retrofit
,所以我对为什么会收到错误感到非常困惑。出于无奈,我删除了在引用模型上添加的SerializedName
,然后它又起作用了。我认为即使没有 SerializedName
我的模型也能工作的原因是因为字段名称本身已经看起来像 json 字段,都是小写和蜗牛大小写。当我添加 SerializedName
时,Retrofit
认为已经是一个额外的 SerializedName
,所以我得到了错误,这似乎有时是由具有相同值的多个 SerializedName
引起的。