我们有一个API,可以将参数作为json / application接收。我有一个Object类,想以json形式发送参数,并通过改造2进入该api。但是我有一个例外 java.lang.IllegalArgumentException:无法为com.account.sampark.ledger.model.UserRegister类(参数#1)创建@Body转换器
public class UserRegister {
private String firstName;
private String lastName;
private String phone1;
private String phone2;
private String emailId;
private String passwd;
private String token;
private int roleId;
public UserRegister(String firstName, String lastName, String phone1, String phone2, String emailId, String passwd, String token) {
this.firstName = firstName;
this.lastName = lastName;
this.phone1 = phone1;
this.phone2 = phone2;
this.emailId = emailId;
this.passwd = passwd;
this.token = token;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public int getRoleId() {
return roleId;
}
}
ApiClient
public class ApiClient {
public static final String BASE_URL = ApiUrls.BASE_URL;
public static Retrofit retrofit = null;
public static Retrofit getApiClient() {
if (retrofit == null) {
Timber.plant(new Timber.DebugTree());
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Timber.i(message);
}
});
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor)
.connectTimeout(3000, TimeUnit.SECONDS).build();
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).
client(client).addConverterFactory(LoganSquareConverterFactory.create()).build();
}
return retrofit;
}
}
@Headers("Content-Type: application/json")
@POST(USER + ApiUrls.CREATE)
Call<UserRole> registerUser(@Body UserRegister parm);
我从这里打电话
Call<UserRole> call = apiMobile.registerUser(params);
例外
Process: com.account.sampark.ledger, PID: 8334
java.lang.IllegalArgumentException: Unable to create @Body converter for class com.account.sampark.ledger.model.UserRegister (parameter #1)
for method ApiMobile.registerUser
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:760)
at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:716)
at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:339)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:207)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:813)
at $Proxy0.registerUser(Unknown Source)
at com.account.sampark.ledger.ui.common.fragment.FragmentRegistrationForm$3.onClick(FragmentRegistrationForm.java:118)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6221)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.lang.IllegalArgumentException: Could not locate RequestBody converter for class com.account.sampark.ledger.model.UserRegister.
Tried:
* retrofit2.BuiltInConverters
* com.github.aurae.retrofit2.LoganSquareConverterFactory
at retrofit2.Retrofit.nextRequestBodyConverter(Retrofit.java:303)
at retrofit2.Retrofit.requestBodyConverter(Retrofit.java:262)
at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:713)
... 14 more
我尝试删除@Headers(“ Content-Type:application / json”),但对我不起作用。See my json/application API here
答案 0 :(得分:0)
您正在使用LoganSquareConverterFactory
,因此需要像这样注释每个字段。查看官方文档Here
@JsonObject
public class UserRegister {
@JsonField
private String firstName;
@JsonField
private String lastName;
@JsonField
private String phone1;
@JsonField
private String phone2;
@JsonField
private String emailId;
@JsonField
private String passwd;
@JsonField
private String token;
@JsonField
private int roleId;
public UserRegister() { }
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public int getRoleId() {
return roleId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}