Retrofit 的 Put 方法在 Android 中不起作用
Put method of Retrofit is not working in Android
我正在尝试使用 Retrofit2 实现一个 put 方法来更新演示的记录 API,但它没有在回调中给我响应并跳转到 onFailure 函数。
- 我正在使用演示 APi (http://dummy.restapiexample.com/update)
- 我有一个响应 class UpdateResponse
- 我有一个 Api class 和一个 Api 接口
- 和一个对话框而不是主要的 activity
- 在Api接口
中使用PUT方法有一个@path(id)和三个@fields(name,salary,age)
UpdateResponse class 代码如下
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 Class 代码如下
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;
}
}
对话框作为主要activity代码如下
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" and in Throwable t it showing like "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 transient
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);
看来你的改造代码没问题。你能分享整个错误日志吗? SPAN_EXCLUSIVE_EXCLUSIVE 我有一个与 EditText 相关的不同问题。
您遇到的错误不是您的代码造成的;您可能正在具有三星 TouchWiz 的三星设备上进行测试。
我遇到了同样的错误,然后我在 Nexus S 上进行了测试(也是三星的,但是没有 TouchWiz 的纯 Android OS),我没有得到这个错误。
我正在尝试使用 Retrofit2 实现一个 put 方法来更新演示的记录 API,但它没有在回调中给我响应并跳转到 onFailure 函数。
- 我正在使用演示 APi (http://dummy.restapiexample.com/update)
- 我有一个响应 class UpdateResponse
- 我有一个 Api class 和一个 Api 接口
- 和一个对话框而不是主要的 activity
- 在Api接口 中使用PUT方法有一个@path(id)和三个@fields(name,salary,age)
UpdateResponse class 代码如下
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 Class 代码如下
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;
}
}
对话框作为主要activity代码如下
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" and in Throwable t it showing like "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 transient
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);
看来你的改造代码没问题。你能分享整个错误日志吗? SPAN_EXCLUSIVE_EXCLUSIVE 我有一个与 EditText 相关的不同问题。
您遇到的错误不是您的代码造成的;您可能正在具有三星 TouchWiz 的三星设备上进行测试。
我遇到了同样的错误,然后我在 Nexus S 上进行了测试(也是三星的,但是没有 TouchWiz 的纯 Android OS),我没有得到这个错误。