在将json对象发送到服务器时,我遇到上述异常, 这是我完整的代码。
RegisterModelQ.java
public class RegisterModelQ {
@SerializedName("name")
private String name;
@SerializedName("mobile")
private String mobile;
@SerializedName("password")
private String password;
@SerializedName("device_token")
private String deviceToken;
@SerializedName("device_type")
private String deviceType;
public RegisterModelQ(String name, String mobile, String password, String device_type, String device_token) {
this.name = name;
this.mobile = mobile;
this.password = password;
this.deviceToken= device_token;
this.deviceType = device_type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ApiInterface.java
@FormUrlEncoded
@POST("user/log-in")
Call<LoginModelR> getLoginAPI(LoginModelQ loginModel);
APIClient.java
public static Retrofit getRetrofit() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EndPoints.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
我的电话
@Override
public void registerCall(RegisterModelQ registerModelQ) {
view.showProgress();
APIInterface apiService = APIClient.getRetrofit().create(APIInterface.class);
JSONObject jsonObject = null;
String string = Utils.getGson().toJson(registerModelQ);
try {
jsonObject = new JSONObject(string);
} catch (JSONException e) {
e.printStackTrace();
}
Call<RegisterModelR> call = apiService.getRegisterAPI(jsonObject);
call.enqueue(new Callback<RegisterModelR>() {
@Override
public void onResponse(Call<RegisterModelR> call, Response<RegisterModelR> response) {
view.hideProgress();
if (response.isSuccessful()) {
view.changeView(new DashboardFragment());
}
}
@Override
public void onFailure(Call<RegisterModelR> call, Throwable t) {
Utils.logD(TAG, "Errrr: " + t.getMessage());
view.hideProgress();
}
});
}
错误日志
java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)
for method APIInterface.getRegisterAPI
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:743)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:761)
at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:351)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:204)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy0.getRegisterAPI(Unknown Source)
at com.codingnation.shoingfest.ui.register.RegisterImpl.registerCall(RegisterImpl.java:61)
at com.codingnation.shoingfest.ui.register.RegisterImpl.validateFields(RegisterImpl.java:46)
at com.codingnation.shoingfest.ui.register.RegisterFragment$1.onClick(RegisterFragment.java:58)
at android.view.View.performClick(View.java:6305)
at android.view.View$PerformClick.run(View.java:24840)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我发现了许多与我有关的问题,但没有解决我的错误, 加上如果我想将Java对象发送到API怎么办。 我应该在服务器端用PHP来实现吗?
答案 0 :(得分:-1)
请遵循以下步骤。
public JSONObject getJsonObject(Object object) {
ObjectMapper mapperObj = new ObjectMapper();
JSONObject jsonObject = null;
try {
String jsonStr = mapperObj.writeValueAsString(object);
try {
jsonObject = new JSONObject(jsonStr);
return jsonObject;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
return jsonObject;
}
APIInterface apiService = APIClient.getRetrofit().create(APIInterface.class);
Call<RegisterModelR> call = apiService.getLoginAPI(RequestBody.create(MediaType.parse("application/json"),
getJsonObject(registerModelQ).toString()));
@POST("user/log-in")
Call<LoginModelR> getLoginAPI(@Body RequestBody requestBody);
祝你好运。