Q:

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 {
@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.

A:

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

