我在youtube视频后学习改造 但是现在我被卡住了。它向我显示了一个错误"改进了预期的begin_array,但是在第1行第2列路径上的begin_object $" 我试图从这个网站获取json数据。 http://servicio-monkydevs.rhcloud.com/clientes/
这是我的代码
MainActivity.java
resultadoTextView = (TextView) findViewById(R.id.Resultado);
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl("http://servicio-monkydevs.rhcloud.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
ClienteService service = restAdapter.create(ClienteService.class);
Call<Cliente> call = service.getCliente();
call.enqueue(new Callback<Cliente>() {
@Override
public void onResponse(Call<Cliente> call, Response<Cliente> response) {
if(response.isSuccessful()) {
resultadoTextView.setText(call.toString());
}else{
resultadoTextView.setText("algo paso");
}
}
@Override
public void onFailure(Call<Cliente> call, Throwable t) {
resultadoTextView.setText(t.getMessage());
}
});
ClientService.java
public interface ClienteService {
@GET("/clientes")
Call<Cliente> getCliente();
}
Client.java
public class Cliente {
private int id;
private String name;
private String username;
private String email;
private String phone;
private String website;
private String photo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
@Override
public String toString() {
return "Cliente{" +
"id=" + id +
", name='" + name + '\'' +
", username='" + username + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", website='" + website + '\'' +
", photo='" + photo + '\'' +
'}';
}}
我做错了什么?
更新
我做了这些修改
public class Cliente {
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("username")
private String username;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("website")
private String website;
@SerializedName("photo")
private String photo;
...
这在界面中
public interface ClienteService {
@GET("/clientes")
Call<List<Cliente>> getCliente();
}
这就像你说的那样在MainActivity中
Call<List<Cliente>> call = service.getCliente();
call.enqueue(new Callback<List<Cliente>>() {
@Override
public void onResponse(Call<List<Cliente>> call, Response<List<Cliente>> response) {
if(response.isSuccessful()) {
resultadoTextView.setText(call.toString());
}else{
resultadoTextView.setText("algo paso");
}
}
@Override
public void onFailure(Call<List<Cliente>> call, Throwable t) {
resultadoTextView.setText(t.getMessage());
}
});
但现在它向我显示了这个错误: 的&#34; retrofit2.executorCallAdapterFactory$ExecutorCallbackCall@6a3dd44"
它在这一行显示了我
...
if(response.isSuccessful()) {
resultadoTextView.setText(call.toString()); <-- HERE
}else{
...
答案 0 :(得分:9)
正如您所看到的给定REST API url返回一个Object数组,即ArrayList,但在您的改装api服务中,返回类型为Only Cliente。 因此,将ClientService.java更改为以下
public interface ClienteService {
@GET("/clientes")
Call<List<Cliente>> getCliente();
}
将Call.enque()方法更改为此
Call<List<Cliente>> call = service.getCliente();
call.enqueue(new Callback<List<Cliente>>() {
@Override
public void onResponse(Call<List<Cliente>> call, Response<List<Cliente>> response) {
if(response.isSuccessful()) {
// your code to get data from the list
}else{
}
}
@Override
public void onFailure(Call<List<Cliente>> call, Throwable t) {
resultadoTextView.setText(t.getMessage());
}
});
答案 1 :(得分:1)
Call<Client>
更改为Call<List<Client>>
,因为响应返回列表对象客户端。Class Client
实施 Serializable
以支持解析我认为已经完成了