简体   繁体   English

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

[英]I am not able to use RestApi in android application

Here is my RestApi get call result RestApi 这是我的RestApi获得通话结果RestApi

An in my android application i am using the retrofit library to call this api to show items in my android application, here is the api call in android 在我的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();
}

When I run my app it gives this error message "HandShake failed" error Please help 当我运行我的应用程序时,它会显示此错误消息“握手失败” 错误,请帮助

And here is logcat view 这是logcat视图

And her is the code to getItems 而她是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();
        }
    });
}

Your problem in https certificate validation. 您在https certificate验证中https certificate问题。 If you want to accept all trused/untrasted certificate then use this client in your project. 如果要接受所有受约束/未受约束的证书,请在项目中使用此客户端。 Your problem will be solved. 您的问题将得到解决。

Retorift builder: 反编译器:

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();

Unsafe client: 不安全的客户:

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);
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM