java 改造预期的 begin_array 但在第 1 行第 2 列路径 $ 处是 begin_object

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38066728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:05:11  来源:igfitidea点击:

retrofit expected begin_array but was begin_object at line 1 column 2 path $

javaandroidrestretrofit

提问by eduarboy

I'm learning retrofit following a youtube video but right now I'm stuck. It shows me an error "retrofit expected begin_array but was begin_object at line 1 column 2 path $" I'm trying to get json data from this site. http://servicio-monkydevs.rhcloud.com/clientes/

我正在按照 youtube 视频学习改造,但现在我被卡住了。它向我显示了一个错误“改造预期的begin_array 但在第 1 行第 2 列路径 $ 处是 begin_object” 我正在尝试从该站点获取 json 数据。 http://servicio-monkydevs.rhcloud.com/clientes/

Here is my code

这是我的代码

MainActivity.java

主活动.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

客户端.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 + '\'' +
            '}';
}}

What am I doing wrong?

我究竟做错了什么?

UPDATE

更新

I made these changes

我做了这些改变

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;
...

And this in the interface

而这个在界面中

public interface ClienteService {
  @GET("/clientes")
  Call<List<Cliente>> getCliente();
}

And this in the MainActivity as you say

正如你所说的,这在 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());
        }
    });

But now it shows me this error: "retrofit2.executorCallAdapterFactory$ExecutorCallbackCall@6a3dd44"

但现在它向我显示了这个错误: “retrofit2.executorCallAdapterFactory$ExecutorCallbackCall@6a3dd44”

It shows me this in this line

它在这一行中向我展示了这一点

...
if(response.isSuccessful()) {
            resultadoTextView.setText(call.toString());  <-- HERE
        }else{
...

回答by BalaramNayak

As you Can see the Given REST API url returning an array of Object , that is ArrayList but in your retrofit api service the return type is Only Cliente. So change your ClientService.java to the below

正如您所看到的, Given REST API url 返回一个 Object 数组,即 ArrayList 但在您的改造 api 服务中,返回类型为 Only Cliente。因此,将您的 ClientService.java 更改为以下内容

public interface ClienteService {
 @GET("/clientes")
 Call<List<Cliente>> getCliente();
}

And change the Call.enque() method to this

并将 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());
        }
    });

回答by ThanhTranIT

  • the firstly you need to change Call<Client>to Call<List<Client>>because response return list object client.
  • the secondly prepare that Class ClientimplementsSerializableto support parsing
  • 首先您需要更改Call<Client>Call<List<Client>>因为响应返回列表对象客户端。
  • 第二个准备Class Client实现Serializable支持解析

I think that done

我认为完成了