android - 改造的放置方法在Android中不起作用
问题描述
我正在尝试使用 Retrofit2 实现一个 put 方法来更新演示 API 的记录,但它没有在回调中给我响应并跳转到 onFailure 函数。
- 我正在使用演示 APi ( http://dummy.restapiexample.com/update )
- 我有一个响应类 UpdateResponse
- 我有一个 Api 类和一个 ApiInterface
- 和一个对话框而不是主要活动
- 在 ApiInterface 中有一个 @path (id) 和三个 @fields(name,salary,age) 使用 PUT 方法
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();
}
});
作为响应电话,它显示:
调用:ExecuterCallAdapterFactory$ExecuterCallbackCall@5922”,在 Throwable 中显示为“com.google.gson.stream.MalformedJsonException:第 1 行第 27 列路径 $.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);
推荐阅读
- macos - ubuntu 和 OSX 上 xattr.h 中函数定义的差异
- reactjs - React ComponentDidMount Get 请求获取 API 信息,但在第 3 次或第 4 次尝试设置状态
- json - 优化基于镜头的 JSON 处理
- vba - VBA Form.Move 方法(访问)- 不适用于拆分表单
- asp.net-mvc - 为什么 React 列表切片总是删除最后一行
- javascript - 如何在 HTML 中打印 JavaScript 函数的返回值?
- c++ - boost small_vector 无法返回使用堆栈空间
- scala - 在Scala`List`中查找由两个元素分隔的所有切片
- c# - 快速二维积分坐标到 C# 中的自定义类字典集合
- swift - 定时器在 Swift 4 的 cocoa/mac OS 中不起作用