我正在学习youtube视频的改装,但现在我卡住了。它显示一个错误“reverfit expected begin_array but was begin_object at line 1 column 2 path$”我正在尝试从这个站点获取json数据。http://servicio-monkydevs.rhcloud.com/clientes/
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();
}
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());
}
});
但现在它显示了这个错误:“reverfit2.executorCallAdapterFactory$executorCallbackCall@6a3dd44”
它在这一行给我看了这个
...
if(response.isSuccessful()) {
resultadoTextView.setText(call.toString()); <-- HERE
}else{
...
正如您可以看到的,给定的REST API url返回一个Object数组,即ArrayList,但在您的改版API服务中,返回类型仅为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());
}
});
错误:java.lang.IllegalStateException:应为begin_array,但为begin_object 我不知道怎么解决这个问题 我在这里包含了我的完整代码 这是我的JSON search_movie.class apiclient.java
因此,在我的应用程序中,用户可以为他们的饭菜拍照,通过使用TensorFlow的图像分类,它将在中对饭菜进行分类。然后,使用Edamam食谱搜索API,它将向用户返回该餐的食谱。 这是我的中的中的内容: 对于、和,我在活动的顶部创建了以下变量: 更新1:我包含了使用后的JSON响应。 更新2:我已经包含了Recipe类。 有人知道如何解决这个问题吗?
在阅读@Ridcully的响应之后,我想问一下是否有一种方法可以更新,以便它知道JSON是一个数组。比如?
我怎么解决这个?
我使用django作为服务器端 在django中,我像这样返回Json 我不知道为什么会产生问题
我做错了什么? 当我这样读json时,它工作得很好