蜂窝网络功能
我正在尝试为社交网站制作 Android Honeycomb 平板电脑应用程序。我创建了一个类来保存各种 API 函数,就像这样:
public String getBlogInfo(String blogName)
它包含 HttpGet 和其他网络事物。我从主线程调用该函数,如下所示:
APIHelper apiHelp = new APIHelper();
String blogInfo = apiHelp.getBlogInfo(blog);
当然,在收到错误 NetworkOnMainThreadException 并做了一些研究后,我发现我需要使用 Handlers 或 AsyncTasks 来正确完成此任务。我真的不知道该如何开始。我希望能在正确的方向上有所推动,如果您需要更多我的代码,我将很乐意提供。
I'm trying to make a Android Honeycomb tablet app for a social website. I created a class to hold various API functions, like this one:
public String getBlogInfo(String blogName)
which contains HttpGet and other networked things. I'm calling that function from the main thread like so:
APIHelper apiHelp = new APIHelper();
String blogInfo = apiHelp.getBlogInfo(blog);
Of course, after receiving the error NetworkOnMainThreadException and doing a little research I found that I need to use Handlers or AsyncTasks to pull this off properly. I just really don't know how I would start. I would love a nudge in the right direction, and if you need more of my code I'll be happy to provide it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Honeycomb 中,您无法在主线程中执行与网络相关的活动。因此,您要么需要为网络相关活动创建一个新线程,要么使用 AsyncTasks。
您可能想查看 http://developer.android.com/resources/ articles/painless-threading.html 或 http://developer.android.com/reference/android/os/AsyncTask.html
In Honeycomb, you cannot perform network related activity in the main thread. So, you either need to create a new thread for network related activity or use AsyncTasks.
You might probably want to look at http://developer.android.com/resources/articles/painless-threading.html or http://developer.android.com/reference/android/os/AsyncTask.html
我在这里找到了一个很好的解决方案:
http://ajeyasharma.com/2010/ 04/returning-values-from-asynctask.html
就像魅力一样
I found a good solution here:
http://ajeyasharma.com/2010/04/returning-values-from-asynctask.html
Works like a charm