【问题标题】:I am not able to use RestApi in android application我无法在 android 应用程序中使用 Rest Api
【发布时间】:2019-01-07 17:30:01
【问题描述】:

这是我的 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

而她是获取物品的代码

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

【问题讨论】:

  • 使用堆栈跟踪发布您的完整错误
  • 还发布 Item 类和 json 响应以及 Logcat 错误
  • Json 响应附为图片
  • 分享您拨打getItems()的代码以及改造电话
  • 可能与改造无关。基本 URL 是否接受 HTTPS 连接?先用http试试看是否有效

标签: android api retrofit retrofit2


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但请您说明如何添加/使用此类?我的意思是我已经在我的项目中创建了这个类,但它不能作为相同的异常“java.security.cert.CertPathValidatorException:找不到证书路径的信任锚”再次出现,请在添加这个类后告诉我我应该在哪里以及如何使用这个/调用它的方法?
  • __trustAllHttpsCertificates() 调用此方法
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 2019-10-21
  • 2014-01-04
  • 2014-10-19
  • 1970-01-01
相关资源
最近更新 更多