【发布时间】: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