Setting Request Priority Volley
Asked Answered
I

3

13

I am trying to set the priority of my requests using the Volley library in Android. I cant find out how to set the requests priority.

StringRequest request = new StringRequest(Request.Method.GET,"feed URL",volleyListener, volleyErrorListener);
pe.requestQueue.add(request);

Any Ideas on how I would do this?

Immigrate answered 14/7, 2013 at 16:7 Comment(0)
J
15

The library unfortunately isn't fully fleshed out yet. To set priority for a request you need to extend the request and override getPriority(). For your example I would create a new class that extends StringRequest and implements getPriority() (and maybe a setPriority() as well, so you can programmatically change priorities in different requests).

private Priority mPriority = Priority.LOW;

@Override
public Priority getPriority() {
    return mPriority;
}

public void setPriority(Priority priority) {
    mPriority = priority;
}

Priority is an ENUM from the Request class.

Jacobjacoba answered 14/7, 2013 at 16:21 Comment(3)
Turns out imagerequest has Priority.LOW set already but you answer works. Thanks.Immigrate
Yes, ImageRequest has it already, but StringRequest does not, and the Request class doesn't provide a method to manually set it. Volley works well as is, but if you want to tailor it to your own uses custom classes are still required.Jacobjacoba
Volley supports four levels of priority - Low, Normal, High and Immediate.Cotidal
D
11

Heres a quick way to set a priority,

    StringRequest request = new StringRequest(Request.Method.GET,"feed URL",volleyListener, volleyErrorListener) {
        @Override
        public Priority getPriority() {
            return Priority.IMMEDIATE;
        }
    };
Deafmute answered 25/9, 2015 at 18:44 Comment(2)
Thank you, this is a much better way of doing a simple thing like just changing the priority.Zweig
is this will set the priority also I am little bit confused can you clarify please..Gallicism
U
0

With 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 } }
Usurer answered 21/12, 2021 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.