Android - Retrofit2 - java.security.cert.CertPathV

2019-08-04 20:01发布

问题:

I want to get data from the server (https://data.egov.kz/api/v2/zheke_zhane_zandy_tulgalardy_k1/v6?pretty) as an array of json objects. But I get this Log: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. I am using Retrofit2 and here my code:

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements GetAdmissionSchedule.GetAdmissionScheduleInterface {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GetAdmissionSchedule getAdmissionSchedule = new GetAdmissionSchedule(this);
        getAdmissionSchedule.getAdmissionScheduleList();
    }

    @Override
    public void getAdmissionSchedule(List<AdmissionSchedule> admissionScheduleList) {
        // here i get my data
    }

}

GetAdmissionSchedule.java

public class GetAdmissionSchedule {

    private GetAdmissionScheduleInterface getAdmissionScheduleInterface;

    public GetAdmissionSchedule(GetAdmissionScheduleInterface getAdmissionScheduleInterface) {
        this.getAdmissionScheduleInterface = getAdmissionScheduleInterface;
    }

    public interface GetAdmissionScheduleInterface {
        void getAdmissionSchedule(List<AdmissionSchedule> admissionScheduleList);
    }

    public void getAdmissionScheduleList() {
        DataEgovApi service = DataEgovBaseURL.getRetrofit();
        Call<List<AdmissionSchedule>> call = service.getAdmissionScheduleList();
        call.enqueue(new Callback<List<AdmissionSchedule>>() {
            @Override
            public void onResponse(Call<List<AdmissionSchedule>> call, Response<List<AdmissionSchedule>> response) {
                Log.d("MyLogs", "MVD: getAdmissionScheduleList " + response.code());
                getAdmissionScheduleInterface.getAdmissionSchedule(response.body());
            }

            @Override
            public void onFailure(Call<List<AdmissionSchedule>> call, Throwable t) {
                Log.d("MyLogs", "MVD: getAdmissionScheduleList " + t.getLocalizedMessage());
                getAdmissionScheduleInterface.getAdmissionSchedule(null);
            }
        });
    }

}

DataEgovBaseURL.java

public class DataEgovBaseURL {

    private static final String BASE_URL = "https://data.egov.kz/";
    private static Retrofit retrofit = null;

    public static DataEgovApi  getRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

        }
        return retrofit.create(DataEgovApi.class);
    }

}

DataEgovApi.java

public interface DataEgovApi {

    @GET("api/v2/zheke_zhane_zandy_tulgalardy_k1/v6?pretty")
    Call<List<AdmissionSchedule>> getAdmissionScheduleList();

}

AdmissionSchedule.java (My POJO class)

public class AdmissionSchedule {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("vremia")
    @Expose
    private String vremia;
    @SerializedName("adres_ru")
    @Expose
    private String adresRu;
    @SerializedName("doljnost_ru")
    @Expose
    private String doljnostRu;
    @SerializedName("name_ru")
    @Expose
    private String nameRu;
    @SerializedName("data")
    @Expose
    private String data;
    @SerializedName("adres_kz")
    @Expose
    private String adresKz;
    @SerializedName("doljnost_kz")
    @Expose
    private String doljnostKz;
    @SerializedName("name_kz")
    @Expose
    private String nameKz;

    public String getId() {
        return id;
    }

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

    public String getVremia() {
        return vremia;
    }

    public void setVremia(String vremia) {
        this.vremia = vremia;
    }

    public String getAdresRu() {
        return adresRu;
    }

    public void setAdresRu(String adresRu) {
        this.adresRu = adresRu;
    }

    public String getDoljnostRu() {
        return doljnostRu;
    }

    public void setDoljnostRu(String doljnostRu) {
        this.doljnostRu = doljnostRu;
    }

    public String getNameRu() {
        return nameRu;
    }

    public void setNameRu(String nameRu) {
        this.nameRu = nameRu;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getAdresKz() {
        return adresKz;
    }

    public void setAdresKz(String adresKz) {
        this.adresKz = adresKz;
    }

    public String getDoljnostKz() {
        return doljnostKz;
    }

    public void setDoljnostKz(String doljnostKz) {
        this.doljnostKz = doljnostKz;
    }

    public String getNameKz() {
        return nameKz;
    }

    public void setNameKz(String nameKz) {
        this.nameKz = nameKz;
    }

}

回答1:

You server url is https and certificate is already not valid.

Change https to http and it will work.

Else you can install valid SSL certificate on the server.