通过 POST 在 Retrofit Expected BEGIN_ARRAY 上发送 ArrayList 时出错,但在第 1 行第 1 列为 STRING
Error sending ArrayList via POST on Retrofit Expected BEGIN_ARRAY but was STRING at line 1 column 1
当我尝试在 Retrofit 2 上使用@POST 将 ArrayList<> 发送到我的网络服务器时,ArrayList<> 保存在数据库中,但在 Android 日志中我得到以下错误:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
我在这里看到了一些关于 SO 的问题,但我无法让它工作,但是我是一个初学者,有点迷路了。
这是 ArrayList:
ArrayList<ModelContact> listContact = new ArrayList<>();
ModelContact c = new ModelContact("5", "OFF", "Test", "12134567", "14646", "email@email.com", "Adress");
listContact.add(c);
c = new ModelContact("6", "ON", "Test2", "12123456", "14646", "email@email.com", "Adress");
listContact.add(c);
这是我的改装电话
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
Log.i("TAG", "Success");
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
这是我的界面:
public interface ContactInterface {
@GET("recieve")
Call<List<ModelContact>> getRContact();
@POST("send")
Call<List<ModelContact>> createRContact(@Body ArrayList<ModelContact> modelContact);
}
当我发出请求时服务器响应:
[{"id":"13","status":"ON","name":"Test","phone":"123","phone2":"1237855","email":"email@email.com","address":"adsfasd"},{"id":"14","status":"OFF","name":"Test","phone":"123","phone2":"125453","email":"email@email.com","address":"adsfasd"}]
ModelContatc class:
public class ModelContact {
String id;
String status;
String name;
String phone;
String phone2;
String email;
String address;
public ModelContact(String id, String status, String name, String phone, String phone2, String email, String address) {
this.id = id;
this.status = status;
this.name = name;
this.phone = phone;
this.phone2 = phone2;
this.email = email;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String nome) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String phone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
正如 Blackbelt 所说,服务器没有响应 json,它的响应来自对数据库的查询,该查询返回 php 错误。
当我尝试在 Retrofit 2 上使用@POST 将 ArrayList<> 发送到我的网络服务器时,ArrayList<> 保存在数据库中,但在 Android 日志中我得到以下错误:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
我在这里看到了一些关于 SO 的问题,但我无法让它工作,但是我是一个初学者,有点迷路了。
这是 ArrayList:
ArrayList<ModelContact> listContact = new ArrayList<>();
ModelContact c = new ModelContact("5", "OFF", "Test", "12134567", "14646", "email@email.com", "Adress");
listContact.add(c);
c = new ModelContact("6", "ON", "Test2", "12123456", "14646", "email@email.com", "Adress");
listContact.add(c);
这是我的改装电话
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
Log.i("TAG", "Success");
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
这是我的界面:
public interface ContactInterface {
@GET("recieve")
Call<List<ModelContact>> getRContact();
@POST("send")
Call<List<ModelContact>> createRContact(@Body ArrayList<ModelContact> modelContact);
}
当我发出请求时服务器响应:
[{"id":"13","status":"ON","name":"Test","phone":"123","phone2":"1237855","email":"email@email.com","address":"adsfasd"},{"id":"14","status":"OFF","name":"Test","phone":"123","phone2":"125453","email":"email@email.com","address":"adsfasd"}]
ModelContatc class:
public class ModelContact {
String id;
String status;
String name;
String phone;
String phone2;
String email;
String address;
public ModelContact(String id, String status, String name, String phone, String phone2, String email, String address) {
this.id = id;
this.status = status;
this.name = name;
this.phone = phone;
this.phone2 = phone2;
this.email = email;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String nome) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String phone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
正如 Blackbelt 所说,服务器没有响应 json,它的响应来自对数据库的查询,该查询返回 php 错误。