错误预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY

时间:2018-07-25 17:32:11

标签: java android json retrofit2 gson

我没有从服务器获取Json。为什么可能呢?
什么是错误的?

err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

api{
        "Id": 1,
        "Name": "ali",
        "Age": 25,
        "Img": "iVBORw0KGgoAAAANSUhEUgAABMsAAATiCAYAAACkzmRcAAAABG..."

}

MainActivity.java

 ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Employee> call = apiInterface.getUser();
 call.enqueue(new Callback<Employee>() {
     @Override
     public void onResponse(Call<Employee> call, Response<Employee> response) {
         String res = response.toString();
         Log.d("response",res);
     }

     @Override
     public void onFailure(Call<Employee> call, Throwable t) {
         String err = t.toString();
         Log.d("err",err );
     }
 });

Employee.java

 public class Employee {

    @SerializedName("Id")
    private int Id ;
    @SerializedName("Name")
    private String Name ;
    @SerializedName("Age")
    private String Age ;
    @SerializedName("Img")
    private String Img;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        this.Id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getAge() {
        return Age;
    }

    public void setAge(String age) {
        Age = age;
    }

    public String getImg() {............

ApiInterface.java

public interface ApiInterface {

    @GET("get")
    Call<Employee> getUser();


}

ApiClient.java

 public class ApiClient {

    private static Retrofit retrofit = null;
    public static final String BASE_URL = "??????????????????";

    public static Retrofit getClient(){
        if ( retrofit == null ){

            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

        }
        return retrofit;
    }
  }

0 个答案:

没有答案