所以我试图使用其REST API连接到我的Magento商店,并使用Retrofit连接到我的android应用中。
出于某种原因,模拟器正在正确获取数据,但是当我在移动设备上测试该应用程序时。它给出了握手失败错误。我不知道问题出在哪里,所以我不知道我需要共享所有东西和代码。您可以问我需要什么。
改造客户端实例:
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://www.sampleUrl.com/";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)//.client(getUnsafeOkHttpClient().build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
拨打片段
Retrofit retrofit2 = RetrofitClientInstance.getRetrofitInstance();
ApiInterface jsonPlaceHolderApi = retrofit2.create(ApiInterface.class);
jsonPlaceHolderApi.getCategories(key).enqueue(new Callback<category>() {
@Override
public void onResponse(Call<category> call, Response<category> response) {
if (!response.isSuccessful()) {
Toast.makeText(getContext(), "Code: +" +response.code(), Toast.LENGTH_SHORT).show();
return;
}
//It gets here for Emulator
categoryList = response.body().getChildren_data();
adapter.setCategoryList(categoryList);
}
@Override
public void onFailure(Call<category> call , Throwable t)
{
//It gets here for the mobile device
Toast.makeText(getActivity(), "onFailure", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});