我正在开发一个应用程序,因为我必须在许多活动中使用某种类型的数据。因此,我尝试做的是制作单例类并在其中定义getProductHeads
的GET方法,这是翻新调用,将列表返回到我在其中调用的活动。
我面临的问题是我第一次启动应用程序时--->开始添加产品活动---调用此方法getProductHeads
返回大小为0的列表,当我尝试时通过返回并再次开始活动来再次执行此操作,它将为我提供数据,并且一直在提供数据,直到应用程序重新启动。
对此我有点陌生,无法获取,已经检查过但找不到太多。下面是我的单例,并在其中调用该方法代码。
public class ProductHeads {
private static ProductHeads mInstance;
public List<ProductHeadsModel> productHeadsModels = new ArrayList<ProductHeadsModel>();
public static ProductHeads getInstance() {
if(mInstance == null) {
if (mInstance == null)
mInstance = new ProductHeads();
}
return mInstance;
}
private ProductHeads() {
// productHeadsModels = new ArrayList<ProductHeadsModel>();
}
public List<ProductHeadsModel> getProductHeads(String licenskey,Context context){
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(40,TimeUnit.SECONDS)
.readTimeout(60,TimeUnit.SECONDS)
.writeTimeout(60,TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
Call<List<ProductHeadsModel>> call = requestInterface.getProductHeads(licenskey);
call.enqueue(new Callback<List<ProductHeadsModel>>() {
@Override
public void onResponse(Call<List<ProductHeadsModel>> call,Response<List<ProductHeadsModel>> response) {
productHeadsModels = response.body();
if (productHeadsModels != null){
}else Toast.makeText(context,"No Product head response found",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<ProductHeadsModel>> call,Throwable t) {
Toast.makeText(context,"Product Heads::::"+t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
return this.productHeadsModels;
}
}
我从我的活动中这样称呼它...
private List<ProductHeadsModel> productHeadsModels;
productHeadsModels = ProductHeads.getInstance().getProductHeads(mlicenseID,AddProduct.this);
我不知道我的方法不好还是问题所在。任何帮助或建议将不胜感激。
谢谢!!!!
:::: EDIT ::::: 这是我的第一次响应数据,看起来不错,但是返回的列表为空
2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app电子/致电: 产品Shap 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:颜色2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL.CALL:完成类型2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:创建日期 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL: 设计器2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:主管2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:价格2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:库存数量 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL: 质量2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app 电子/电话:别名代码2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL.CALL:评论2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:CBM 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:重量 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:CFT 2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL: 负载能力20'2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:可装载40'2019-11-04 13:38:03.011 4153-4153 / com.ht.dell.qr_app E / CALL:可装载40'HC