Android蜂巢网络操作
我遇到了这个问题,我似乎找不到解决方法。
我正在为 Honeycomb 平板电脑开发一个 Android 应用程序,它需要连接到互联网,下载一些数据,然后在完成后更新 UI。然而,问题是我需要连续执行任意给定的次数。我现在的问题是,我在新线程上使用 Runnable ,并使用 Handler 在进行网络调用时更新 UI。
我的大问题是我无法确定数据是否确实已下载到设备。我所知道的是进行了网络调用,仅此而已。我无法确定是否已下载任何内容。
在我的例子中,最大的问题在于您无法在 Honeycomb 中使用主线程进行网络操作。如果我一直使用主线程,
我真的很希望在这个主题上得到一些帮助。谢谢。
更新: 我将尝试更好地解释一切:
我创建了一个类,该类在单独的线程中进行网络调用,并且该类还包含一些从这些网络调用返回各种数据的方法。但是,网络调用的长度可能会有所不同 - 取决于用户的网络速度 - 因此,如果我调用其中一种方法,即 getTitle(),我无法确定网络调用是否已完成。
我需要做一些类似的事情: '- 创建类的新实例(这是在新线程中下载) - 确保下载已完成,以便我可以使用其中的数据 - 更新进度条'
这基本上就是我需要做的。希望有帮助。谢谢
I've got this problem that I just can't seem to find a way around.
I'm working on an Android app for Honeycomb tablets, and it needs to connect to the internet, download some data and then update the UI once it's done. The thing is, however, that I need to do this any given number of times in succession. My problem right now is that I'm using a Runnable on a new thread with a Handler that updates the UI when the network calls have been made.
My big issue is that I cannot be sure that the data has actually been downloaded to the device yet. All I know is that the network calls are made, and that's it. I cannot be sure if anything's been downloaded.
The big problem in my case lies in the fact that you cannot use the main thread for your network operations in Honeycomb. If I had been using the main thread,
I'd really love some help on this subject. Thanks.
UPDATE:
I'll try to explain everything a bit better:
I've created a class that makes the network calls in a separate thread, and this class also contains a few methods that return various data from those network calls. However, the network calls can vary in length - depending on the user's network speed - so if I call one of the methods, i.e. getTitle(), I can't be sure that the network call has finished yet.
I need to do something along the lines of this:
'- Create new instance of the class (this beings a download in a new thread)
- Make sure that the download has completed, so I can use the data from it
- Update a progressbar'
That's basically what I need to do. Hope it helped. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说真的没有意义。那么您正在拨打网络电话,您收到响应了吗?您正在存储数据吗?
您实际上不应该使用主线程进行网络连接,您是否希望用户在这些网络调用发生时完全瘫痪?
例如,如果您使用 AsyncTask,则
onPostExecute(Object对象)
当您的调用完成时被调用,您可以使用它以菊花链方式连接这些网络调用...This doesn't really make sense to me. So you're making the network calls, are you getting a response? Are you storing the data?
You're not really supposed to use the main thread for network connections, do you want the user to be completely paralyzed while these network calls are happening?
If you use an AsyncTask for example, the
onPostExecute(Object object)
gets called when your calls are done and you can daisy chain those network calls using that...