如何等待凌空响应来完成它在intentservice中的工作?(how to wait the volley response to finish it's work inside inte

编程入门 行业动态 更新时间:2024-10-27 22:33:26
如何等待凌空响应来完成它在intentservice中的工作?(how to wait the volley response to finish it's work inside intentservice?)

使用intentservice在后台使用“Google Volley”获取7 Rss Feed链接的数据并使用ResultReceiver获取结果,但是我无法配置如何等待排队响应来完成它的工作ResultReceiver显示MainActivity中的数据

Working with intentservice to get the data of 7 Rss Feed links with using " Google Volley " in the background and use ResultReceiver to get the result , But I can't configure how to wait on the volley response to finish it's job to fire flag with ResultReceiver to display the data in the MainActivity

最满意答案

你不应该等待它。 有两种方法可以发出网络请求:同步和异步。 如果使用同步,则不会等待结果,因为网络呼叫是阻止操作。

如果你想要这个方向,请参考这个: 我可以用齐射进行同步请求吗?

如果你想使其成为异步的,那么你只需要开始一个请求,然后在请求完成后使用你的ResultReceiver在Response.Listener回调方法中。

有关详细信息,请参阅此处: https : //guides.codepath.com/android/Networking-with-the-Volley-Library

如果您仍然认为应该阻止当前线程并等待结果,则应使用CountDownLatch 。 创建一个后台线程(你不能在android中阻塞UI线程,除非它是一个单独的进程),用count 1初始化你的锁存器并调用await()方法。 这将阻止你的后台线程,直到计数为0.一旦你的后台任务完成,你可以调用countDown()来解除后台线程的阻塞,然后你就可以执行你的动作了。

You shouldn't wait for it. You have two ways to make a network request: synchronous and asynchronous. If you use synchronous, you don't wait for result because the network call is a blocking operation.

Refer this if you want to go this direction: Can I do a synchronous request with volley?

If you want to make it asynchronous, you just start a request and then use your ResultReceiver in a Response.Listener callback method once the request is finished.

Refer this for the details: https://guides.codepath.com/android/Networking-with-the-Volley-Library

If you still think you should block the current thread and wait for result, you should use CountDownLatch. Create a background thread (you cannot block UI thread in android unless it's a separate process), init your latch with count 1 and call await() method. This will block your background thread till the count is 0. Once your background task is finished you call countDown() which will unblock your background thread and you will be able to perform your action then.

更多推荐

本文发布于:2023-08-01 17:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:它在   来完成   工作   intentservice   wait

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!