As all requests in Volley are executed asynchronously on a different thread without blocking the “main thread”, is there any way to wait for the request to complete and then continue the main thread execution?
-
`wait for the request to complete and then continue the main thread execution?` you mean block the main thread? that's not recommended, no. (that's the whole point, btw.) – njzk2 Feb 06 '14 at 21:29
-
actually i need to process the response immediately after the request, otherwise i get null pointer exception , or may be the main thread could check weather the response has been received and then continue??? – Sameed Feb 06 '14 at 21:34
-
no. you need to not depend on the request completion, and only do what you have to do when the request is completed, in the Listener that you pass when you start the request. – njzk2 Feb 06 '14 at 21:35
-
well that's awkward to say that i don't have to depend on the request completion – Sameed Feb 06 '14 at 21:40
-
not really. you start a request somewhere in your main thread. you are not going to sit there and wait 10 seconds for the result to come, preventing any ui updates, are you? you put the code that requires the request completion in the listener, and voila. – njzk2 Feb 06 '14 at 21:42
-
is it feasible to start another request in the listener of a request response? just wondering.. – Sameed Feb 06 '14 at 22:47
-
yes, it is. it is how it is intended to be done. – njzk2 Feb 07 '14 at 02:19
2 Answers
You should design your app such that it keeps the main thread live at all times. You can then have blocks of code run when the response has been received using listeners or async task. Check out my answer using listeners here. Or look at onPostExecute for AsyncTask.
is there any way to wait for the request to complete and then continue the main thread execution?
An alternative is to show a loading dialog box while volley is working. This way you can prevent the user from interacting with your app until the request is completed (just don't forget to give him a chance to cancel).
Volley is meant to work in parallel with your main thread and tell you when the request has completed (what all apps should do) if you don't desire that, don't use Volley. But you'll get a NetworkOnMainThreadException and if you manage to bypass it, you'll end up with an ANR exception.

- 1
- 1

- 16,865
- 6
- 62
- 72