I'm trying to implement a put method using Retrofit2 to update records of a demo API, but it's not giving me response in callback and jump into onFailure function.
UpdateResponse class code is below
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 Interface code is below
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 Class code is below
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;
}
}
And Dialog box as main activity code is below
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();
}
});
In response call it's showing:
call: ExecuterCallAdapterFactory$ExecuterCallbackCall@5922" and in Throwable t it showing like "com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 27 path $.error.
You need to send a json body
Change your api method signature like below
@Headers({"Content-Type: application/json"})
@PUT("api/v1/update/{id}")
Call<ResponseBody> updateUser(@Path("id") int id, @Body UpdateResponse body);
Make id transient
public class UpdateResponse {
@SerializedName("id")
@Expose
private transient int id;
//..
Pass data in the body
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With