我无法在Android应用程序中使用RestApi

时间:2018-07-31 10:34:09

标签: android api retrofit retrofit2

这是我的RestApi收到通话结果 RestApi

在我的android应用程序中,我正在使用改造库调用此api来显示我的android应用程序中的项目,这是android中的api调用

package com.aditmsolutions.www.foodie;
import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

public interface Api {
    String BASE_URL = "https://10.0.2.2:51087/api/";
    @GET("items")
    Call<List<Item>> getItems();
}

当我运行我的应用程序时,它显示此错误消息“握手失败” error 请帮助

这是logcat view

她是getItems的代码

private void getItems() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are      using the GsonConverterFactory to directly convert json data to object
            .build();

    Api api = retrofit.create(Api.class);

    Call<List<Item>> call = api.getItems();

    call.enqueue(new Callback<List<Item>>() {
        @Override
        public void onResponse(Call<List<Item>> call, Response<List<Item>>   response) {
            List<Item> itemList = response.body();

            //Creating an String array for the ListView
            String[] items = new String[itemList.size()];

            //looping through all the heroes and inserting the names inside the string array
            for (int i = 0; i < itemList.size(); i++) {
                items[i] = itemList.get(i).getTitle();
            }


            //displaying the string array into listview
            listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items));

        }

        @Override
        public void onFailure(Call<List<Item>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您在https certificate验证中遇到的问题。如果要接受所有受约束/未受约束的证书,请在项目中使用此客户端。您的问题将得到解决。

修正生成器:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are      using the GsonConverterFactory to directly convert json data to object
            .client(UnsafeOkHttpClient.getUnsafeOkHttpClient())
            .build();

不安全的客户端:

public class UnsafeOkHttpClient {  
public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                }
        };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
}