【Volley】Volley的网络请求有内存溢出现象

官网上有写,然后创建RequestQueue 的时候用Application的context。 If your application
makes constant use of the network, it’s probably most efficient to set
up a single instance of RequestQueue that will last the lifetime of
your app. You can achieve this in various ways. The recommended
approach is to implement a singleton class that encapsulates
RequestQueue and other Volley functionality. Another approach is to
subclass Application and set up the RequestQueue in
Application.onCreate(). But this approach is discouraged; a static
singleton can provide the same functionality in a more modular way.

A key concept is that the RequestQueue must be instantiated with the
Application context, not an Activity context. This ensures that the
RequestQueue will last for the lifetime of your app, instead of being
recreated every time the activity is recreated (for example, when the
user rotates the device).

import android.content.Context;  
import com.android.volley.RequestQueue;  
import com.android.volley.toolbox.ImageLoader;  
import com.android.volley.toolbox.Volley;  
  
public class ByklVolley {  
    private static RequestQueue mRequestQueue;  
    private static ByklVolley mInstance;  
    private Context context;  
  
    private ByklVolley(Context context) {  
        this.context = context;  
        mRequestQueue = getRequestQueue();  
    }  
  
    public static synchronized ByklVolley getInstance(Context context) {  
        if (mInstance == null) {  
            mInstance = new ByklVolley(context);  
        }  
        return mInstance;  
    }  
  
    // public static void init(Context context) {  
    // if (mRequestQueue == null) {  
    // /**  
    // * Volley的网络请求有内存溢出现象 整个项目中用一个RequestQueue  
    // * ,可以弄成单例模式。官网上有写,然后创建RequestQueue 的时候用Application的context。If your  
    // * application makes constant use of the network, it's probably most  
    // * efficient to set up a single instance of RequestQueue that will  
    // * last the lifetime of your app. You can achieve this in various  
    // * ways. The recommended approach is to implement a singleton class  
    // * that encapsulates RequestQueue and other Volley functionality.  
    // * Another approach is to subclass Application and set up the  
    // * RequestQueue in Application.onCreate(). But this approach is  
    // * discouraged; a static singleton can provide the same  
    // * functionality in a more modular way.  
    // *  
    // * A key concept is that the RequestQueue must be instantiated with  
    // * the Application context, not an Activity context. This ensures  
    // * that the RequestQueue will last for the lifetime of your app,  
    // * instead of being recreated every time the activity is recreated  
    // * (for example, when the user rotates the device).  
    // */  
    // mRequestQueue = Volley.newRequestQueue(context  
    // .getApplicationContext());  
    // }  
    // }  
  
    public RequestQueue getRequestQueue() {  
        if (mRequestQueue == null) {  
            // getApplicationContext() is key, it keeps you from leaking the  
            // Activity or BroadcastReceiver if someone passes one in.  
            mRequestQueue = Volley.newRequestQueue(context  
                    .getApplicationContext());  
        }  
        return mRequestQueue;  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值