java.lang.UnsupportedOperationException: 接口无法实例化!接口名称:com.google.android.gms.location.places.Place

java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: com.google.android.gms.location.places.Place

我发送原始 json 改造体。 json 请求对象在邮递员中工作正常,但经过改造后会抛出异常。 这是我的界面

public interface RetrofitApi {

    @Headers("Content-Type: application/json")
    @POST("/cook/list")
    Call<GetNearByCooks> getCooksNearBy(@Body String object);
}

这是模特class

public class GetNearByCooks implements Serializable {
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("status_code")
    @Expose
    private int status_code;

    @SerializedName("data")
    @Expose
    private List<CookDetails> cookList;

    public GetNearByCooks(String status, int status_code, List<CookDetails> cookList) {
        this.status = status;
        this.status_code = status_code;
        this.cookList = cookList;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getStatus_code() {
        return status_code;
    }

    public void setStatus_code(int status_code) {
        this.status_code = status_code;
    }

    public List<CookDetails> getCookList() {
        return cookList;
    }

    public void setCookList(List<CookDetails> cookList) {
        this.cookList = cookList;
    }
}

这是 CookDetails class

public class CookDetails implements Serializable {
    @SerializedName("id")
    @Expose
    int id;

    @SerializedName("full_name")
    @Expose
    String name;

    @SerializedName("email")
    @Expose
    String email;

    @SerializedName("phone")
    @Expose
    String phone;

    @SerializedName("profile_image_url")
    @Expose
    String imageUrl;

    @SerializedName("service_type")
    @Expose
    int serviceType;

    @SerializedName("address")
    @Expose
    String address;

    @SerializedName("area_detail")
    @Expose
    Place area_detail;

    @SerializedName("logitude")
    @Expose
    String longitude;

    @SerializedName("latitude")
    @Expose
    String latitude;

    @SerializedName("startTime")
    @Expose
    String startTime;

    @SerializedName("endTime")
    @Expose
    String endTime;

    @SerializedName("menu")
    @Expose
    List<CookMenuResponse> menuResponseList;

    public List<CookMenuResponse> getMenuResponseList() {
        return menuResponseList;
    }

    public void setMenuResponseList(List<CookMenuResponse> menuResponseList) {
        this.menuResponseList = menuResponseList;
    }

    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 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 getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public int getServiceType() {
        return serviceType;
    }

    public void setServiceType(int serviceType) {
        this.serviceType = serviceType;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Place getArea_detail() {
        return area_detail;
    }

    public void setArea_detail(Place area_detail) {
        this.area_detail = area_detail;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}

这是我调用函数的地方

private void getCooksList() {
    RetrofitApi retrofitApi = RetrofitClient.getRetrofitClient().create(RetrofitApi.class);
    Log.i(" TAG FoodieHome check",""+jsonRequest.toString());
    retrofitApi.getCooksNearBy(jsonRequest.toString()).enqueue(new Callback<GetNearByCooks>() {
        @Override
        public void onResponse(Call<GetNearByCooks> call, Response<GetNearByCooks> response) {

            if (response.isSuccessful())
            {
                refreshLayout.setRefreshing(false);
                if (response.body().getStatus_code() == 200 && response.body().getStatus().equalsIgnoreCase("Success"))
                {
                    Log.i("TAG Response","in response");
                }
            }
            Log.i("TAG Response","in response\n"+response.body().getStatus()+"\n"+response.body().getStatus_code());

        }

        @Override
        public void onFailure(Call<GetNearByCooks> call, Throwable t) {
            refreshLayout.setRefreshing(false);
            Log.i("TAG Response","in error response\n"+t.getCause());
        }
    });
}

改装客户端

public class RetrofitClient {


  private static Retrofit retrofit = null;

    public static Retrofit getRetrofitClient()
    {
        if (retrofit == null)
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl(Config.BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }

}

最后这是我的 logcat 结果

I/ TAG FoodieHome check: {"device_type":"android","os_type":"android","os_version":"21","build_version":"1.0","token":"d86b6e42-c24d-4fc6-a0f9-565b0cf5f9b9","logitude":"74.23893","latitude":"31.454477","data":{"logitude":"74.23893","latitude":"31.454477","radius":20}}
I/TAG Response: in error response                                                                        java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: com.google.android.gms.location.places.Place

这是我的 Gradle 文件

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:27.0.2'
    compile 'com.android.volley:volley:1.1.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.hbb20:ccp:2.1.4'
    compile 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.facebook.android:facebook-login:4.30.0'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.github.GrenderG:Toasty:1.2.8'
    compile 'com.android.support:design:27.0.2'
    compile "com.google.android.gms:play-services-location:11.8.0"
    compile "com.google.android.gms:play-services-maps:11.8.0"
    compile "com.google.android.gms:play-services-places:11.8.0"
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }
}

在您正在使用的 CookDetails 对象中:

@SerializedName("area_detail")
@Expose
Place area_detail;

Gson 不知道要实例化哪个对象,因为 Place 是一个接口。

因此创建您自己的 Place 对象:

public class MyPlace implements Place{
  // your implementation
}

然后在 CookDetails 对象中执行:

@SerializedName("area_detail")
@Expose
MyPlace area_detail;