我无法在android应用程序中使用RestApi
这是我的RestApi get调用结果 在我的android应用程序中,我正在使用改装库调用此api来显示我的android应用程序中的项目,下面是android中的api调用我无法在android应用程序中使用RestApi,android,api,retrofit,retrofit2,Android,Api,Retrofit,Retrofit2,这是我的RestApi get调用结果 在我的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 = "
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();
}
package com.aditmsolutions.www.foodie;
导入java.util.List;
2.电话;;
导入文件2.http.GET;
公共接口Api{
字符串BASE_URL=”https://10.0.2.2:51087/api/";
@获取(“项目”)
调用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();
}
});
}
private void getItems(){
改装改装=新改装.Builder()
.baseUrl(Api.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())//这里我们使用GsonConverterFactory将json数据直接转换为对象
.build();
Api=改装.create(Api.class);
Call Call=api.getItems();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
List itemList=response.body();
//为ListView创建字符串数组
String[]items=新字符串[itemList.size()];
//循环遍历所有英雄并在字符串数组中插入名称
对于(int i=0;i
您在验证https证书时遇到的问题。如果您想要接受所有的捆绑/未测试的证书,那么在您的项目中使用该客户机。你的问题会解决的
重新提升生成器:
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);
}
}
}
使用stacktraceAlso post Item类和json响应发布完整错误,Logcat errorJson响应以图片形式附加。显示您正在调用的代码
getItems()
,以及可能与改装无关的改装调用。基本URL是否接受HTTPS连接?首先尝试使用http,看看它是否有效谢谢你的回答,但是你能澄清应该如何添加/使用这个类吗?我的意思是我已经在我的项目中创建了这个类,但它并没有像“java.security.cert.CertPathValidatorException:Trust anchor for certification path not found”一样工作,再次出现,请在添加这个类后告诉我应该在哪里以及如何使用这个/调用它的方法?\uu TrustAllHttpSceCertificates()调用这个方法