java 改造:无法为类创建@Body 转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35880907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit: Unable to create @Body converter for class
提问by neustart47
I need to send next json via retrofit 2:
我需要通过改造 2 发送下一个 json:
{
"Inspection": {
"UUID": "name",
"ModifiedTime": "2016-03-09T01:13",
"CreatedTime": "2016-03-09T01:13",
"ReviewedWith": "name2",
"Type": 1,
"Project": {
"Id": 41
},
"ActionTypes": [1]
}
}
With Header: Authorization: access_token_value
带标题: Authorization: access_token_value
I tried this:
我试过这个:
//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();
But everytime i recieved exception: java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)
但每次我收到异常: java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)
How can I send it ? Or any another idea how I can do it. You can even offer me with parameter as simple String
with json inside. It will suit for me
我该如何发送?或任何其他想法我怎么能做到这一点。您甚至可以为我提供String
与 json一样简单的参数。它会适合我
回答by neustart47
Solution:declare body value in your interface with next:
解决方案:在你的界面中声明 body 值,下一步:
@Body RequestBody body
and wrap String JSON object:
@Body RequestBody body
并包装 String JSON 对象:
RequestBody body = RequestBody.create(MediaType.parse("application/json"), obj.toString());
RequestBody body = RequestBody.create(MediaType.parse("application/json"), obj.toString());
回答by Emanuel
You can specify a Converter when you create the Retrofit like this
您可以在像这样创建 Retrofit 时指定一个转换器
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseurl)
.client(okHttpClient)
.build();
回答by Nikson Kanti Paul
Body
uses a single request object, declare your request object as following
Body
使用单个请求对象,如下声明您的请求对象
class Inspection {
String UUID;
//..... add your fields
Project project;
}
class Product
{
int Id;
//....... add your fields
}
I assume your service IConstructSecureAPI
endpoint is:
我假设您的服务IConstructSecureAPI
端点是:
@GET(...) // change based on your api GET/POST
Call<Response> addInspection(
@Header("Authorization") String accesstoken,
@Body Inspection request
);
and you can declare your desire Response
.
你可以宣布你的愿望Response
。
Check this answer, it uses HashMap
instead of class.
检查这个答案,它使用HashMap
而不是类。
回答by Mohd Qasim
there is chance of you kept same @SerializedName("") for multiple vairable/fields/tags
您有可能为多个变量/字段/标签保留相同的 @SerializedName("")
回答by Darien Alvarez
You can use an Interceptor to send Authorization Header in each request
您可以使用拦截器在每个请求中发送授权标头
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);
}
}