正确的方法来制作凌空单打 [英] Proper way to build a Volley Singleton

查看:69
本文介绍了正确的方法来制作凌空单打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复问题"未提供对此问题的适用答案.此处提供的方法不会同时消除两种警告,而只能消除一种.我的问题还包含第二部分,也没有在那里回答.

"Duplicate question" does not provide an applicable answer to this question. The approach provided there does NOT get rid of both warnings, only of one. Also my question contains a 2nd part which is not answered there either.

如果我根据官方教程制作了Volley Singleton,

If I build a Volley Singleton according to the official tutorial,

https://developer.android.com/training/volley/requestqueue.html#singleton

我收到

private static VolleySingleton mInstance 

private static Context mContext 

说不要将android上下文类放在静态字段中".我已经阅读了有关此问题的其他Stackoverflow问题,但是我发现没有解决方法可以摆脱此警告或答案,而该答案实际上可以解释这里发生的情况.是的,我已经阅读了这篇文章:警告:请勿将Android上下文类放置在静态字段中;这是内存泄漏(并且还会中断即时运行)但是我无法提取出适用的答案.我实际上必须更改什么,才能不中断即时运行或在此处泄漏某些内容?

saying "do not place android context classes in static fields". I have read the other Stackoverflow questions about this, but I found no solution that gets rid of this warning or an answer that actually explains what is going on here. Yes I have read this post: Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) But I could not extract an applicable answer. What do I actually have to change to NOT break instant run or leak something here?

我不明白的另一件事是,为什么我必须在

Another thing I dont understand is, why I have to use <T>at

public <T> void addToRequestQueue(Request <T> request)

我知道这代表泛型,但是如果删除它们,它仍然可以使用.那么它在那里的原因是什么?

I know that this stands for Generics, but if I remove them, it still works. What is the reason it is there then?

这是我的Volley Singleton,请有人建议我正确构建它而没有警告和冗余:

This is my Volley Singleton, please someone advice me to build it properly without warnings and redundancy:

public class VolleySingleton {
private static VolleySingleton mInstance;
private static Context mContext;
private RequestQueue mRequestQueue;

private VolleySingleton(Context context) {
    mContext = context;
    mRequestQueue = getRequestQueue();
}

public static synchronized VolleySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new VolleySingleton(context);
    }

    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request <T> request) {
    getRequestQueue().add(request);
}
}

推荐答案

请注意,此重复项.我遵循了在注释中链接的问题中提出的建议,并设法在Android Studio上编写了所需的类而没有任何棉绒警告.这是一个代码段:

Just to be clear, this is a duplicate. I followed the suggestions made in the question that was linked in the comments and managed to write the class you need without any lint warnings on Android Studio. Here is a snippet:

public class VolleySingleton {

  private static VolleySingleton instance;
  private RequestQueue requestQueue;

  private VolleySingleton(Context context) {
    requestQueue = Volley.newRequestQueue(context.getApplicationContext());
  }

  public static VolleySingleton getInstance(Context context) {
    if (instance == null) {
      instance = new VolleySingleton(context);
    }
    return  instance;
  }

  public RequestQueue getRequestQueue(){
    return requestQueue;
  }
}

这篇关于正确的方法来制作凌空单打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆