Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Put method of Retrofit is not working in Android

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.

  • I'm using demo APi (http://dummy.restapiexample.com/update)
  • I have a response class UpdateResponse
  • I have a Api class and a ApiInterface
  • and a dialog box instead of main activity
  • there is one @path (id) and three @fields(name,salary,age) using PUT method in ApiInterface

UpdateResponse class code is below

public class UpdateResponse {
private int id;
private String name;
private String salary;
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 {

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()

    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>() {
                    public void onResponse(Call<UpdateResponse> call, Response<UpdateResponse> response) {
                        Toast.makeText(c.getApplicationContext(),"Updated Name: "+response.body().getName(),Toast.LENGTH_LONG).show();

                    public void onFailure(Call<UpdateResponse> call, Throwable t) {

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.

like image 280
Awais Aslam Avatar asked Mar 04 '23 12:03

Awais Aslam

1 Answers

You need to send a json body

Change your api method signature like below

@Headers({"Content-Type: application/json"})
Call<ResponseBody> updateUser(@Path("id") int id, @Body UpdateResponse body);

Make id transient

public class UpdateResponse {
    private transient int id;

Pass data in the body

 UpdateResponse updateResponse = new UpdateResponse();

 Call<UpdateResponse> call= Api.getClient().updateUser(idd, updateResponse);
like image 123
shb Avatar answered Mar 14 '23 21:03