[英]Put & Delete method not working in Retrofit and rxjava Android
[英]Put method of Retrofit is not working in Android
我正在嘗試使用 Retrofit2 實現一個 put 方法來更新演示 API 的記錄,但它沒有在回調中給我響應並跳轉到 onFailure 函數。
UpdateResponse 類代碼如下
public class UpdateResponse {
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("salary")
@Expose
private String salary;
@SerializedName("age")
@Expose
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String employeeName) {
this.name = employeeName;
}
public String getSalary() {
return salary;
}
public void setSalary(String employeeSalary) {
this.salary = employeeSalary;
}
public String getAge() {
return age;
}
public void setAge(String employeeAge) {
this.age = employeeAge;
}
}
Api接口代碼如下
public interface ApiInterface {
@FormUrlEncoded
@PUT("api/v1/update/{id}")
Call<UpdateResponse> updateUser(@Path("id") int id,
@Field("name") String name,
@Field("salary") String salary,
@Field("age") String age);
}
Api類代碼如下
public class Api {
private static Retrofit retrofit = null;
public static ApiInterface getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl("http://dummy.restapiexample.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
ApiInterface api = retrofit.create(ApiInterface.class);
return api;
}
}
對話框作為主要活動代碼如下
String nameStr = name.getText().toString();
String salaryStr = salary.getText().toString();
String ageStr = age.getText().toString();
//idd is getting from mainActivity onitemSelect method, which is having the right id value
Call<UpdateResponse> call= Api.getClient().updateUser(idd,nameStr,salaryStr,ageStr);
call.enqueue(new Callback<UpdateResponse>() {
@Override
public void onResponse(Call<UpdateResponse> call, Response<UpdateResponse> response) {
Toast.makeText(c.getApplicationContext(),"Updated Name: "+response.body().getName(),Toast.LENGTH_LONG).show();
dismiss();
}
@Override
public void onFailure(Call<UpdateResponse> call, Throwable t) {
Toast.makeText(c.getApplicationContext(),"Failure",Toast.LENGTH_LONG).show();
dismiss();
}
});
在響應呼叫它顯示:
call: ExecuterCallAdapterFactory$ExecuterCallbackCall@5922" 並在 Throwable 中顯示為 "com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 27 path $.error.
您需要發送一個 json 正文
更改您的 api 方法簽名,如下所示
@Headers({"Content-Type: application/json"})
@PUT("api/v1/update/{id}")
Call<ResponseBody> updateUser(@Path("id") int id, @Body UpdateResponse body);
使 id 成為瞬態
public class UpdateResponse {
@SerializedName("id")
@Expose
private transient int id;
//..
在正文中傳遞數據
UpdateResponse updateResponse = new UpdateResponse();
updateResponse.setName(name.getText().toString());
updateResponse.setSalary(salary.getText().toString());
updateResponse.setAge(age.getText().toString());
Call<UpdateResponse> call= Api.getClient().updateUser(idd, updateResponse);
看來你的改造代碼很好。 您能否與錯誤共享整個日志? 我有一個與 EditText 相關的 SPAN_EXCLUSIVE_EXCLUSIVE 的不同問題。
你得到的錯誤不是你的代碼的結果; 您可能正在具有三星 TouchWiz 的三星設備上進行測試。
我遇到了同樣的錯誤,然后我在 Nexus S(也是三星的,但沒有 TouchWiz 的純 Android 操作系統)上進行了測試,但我沒有收到此錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.