我正在尝试使用Android中的Volley库设置我的请求优先级,但我无法找到如何设置请求优先级的方法。
StringRequest request = new StringRequest(Request.Method.GET,"feed URL",volleyListener, volleyErrorListener);
pe.requestQueue.add(request);
你有任何想法如何做到这一点吗?
我正在尝试使用Android中的Volley库设置我的请求优先级,但我无法找到如何设置请求优先级的方法。
StringRequest request = new StringRequest(Request.Method.GET,"feed URL",volleyListener, volleyErrorListener);
pe.requestQueue.add(request);
你有任何想法如何做到这一点吗?
很遗憾,这个库还没有完全发展起来。要为一个请求设置优先级,您需要扩展该请求并覆盖getPriority()函数。对于您的示例,我会创建一个新的类,该类扩展了StringRequest并实现了getPriority()函数(可能还会有setPriority()函数,以便您可以在不同的请求中通过编程方式更改优先级)。
private Priority mPriority = Priority.LOW;
@Override
public Priority getPriority() {
return mPriority;
}
public void setPriority(Priority priority) {
mPriority = priority;
}
Priority
是来自 Request
类的一个枚举类型。
StringRequest request = new StringRequest(Request.Method.GET,"feed URL",volleyListener, volleyErrorListener) {
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
使用 Kotlin:
val stringRequest = object : StringRequest( Request.Method.GET, url,
{ response -> Log.i("Response", response) },
{ error -> Log.i("Error", error) } )
{ override fun getPriority(): Priority { return Priority.HIGH } }