- Home /
Retrieving data automatically from blog website
I'm trying to add notifications in my game that will update automatically when a new blog post is published. Is there a way to do this? I'm fairly new to web integration, so explain it to me like I'm a four-year-old. I don't exactly need an entire script for this, just an explanation with examples. Thanks.
Answer by ttRevan · Oct 31, 2015 at 06:58 AM
You may use a constant polling:
void Start() {
StartCoroutine(StartBlogMonitoring());
}
IEnumerator StartBlogMonitoring() {
while (true) {
var rq = new WWW("http://myblog.com/blogposts-list");
yield return rq;
var list = Parse(rq.text);
if (HasNewPost(list))
{
PushNotification();
}
yield return new WaitForSeconds(2);
}
}
It will send a request to a blog site every 2 seconds to retrieve posts list, then check for a new post there and create notification. Instead of posts list, you can use whatever way is appropriate for your blog to check for new posts.
How should I state these variables?
error CS0103: The name 'Parse' does not exist in the current context
error CS0103: The name 'HasNewPost' does not exist in the current context
error CS0103: The name 'PushNotification' does not exist in the current context
Well, this is just a placeholder methods I use, you need to write them on your own, depending on what format you use, how you decide that blog post has been created and how you add a notification in your scene.
I understand that, but what are Parse and HasNewPost doing? Should list be gathering copies of rq and then HasNewPost is checking for differences between the two most recent? And then what is Parse doing? I just have no idea; please explain. Sorry if I'm pestering you.
Suppose you have a blogging platform that allow you to send request to get the list of all blogs, or just updates, with dates (like rss) in some format (say json or xml). Then
var rq = new WWW("http://myblog.com/blogposts-list");
yield return rq;
will execute this request and wait for response and rq.text
will contain the response as a string. But working with string directly is very difficult, so you need to parse it to some objects, which you can easily query for properties. And that what Parse method is doing. Exact implementation depends on response format and the library you chose to parse it. For json you have Json.NET, for xml there are a lot of ways to parse it too, however I can't recommend any particular.
So, when you have your response parsed, you need a way to find out which posts are new. And HasNewPost should do it, and yes, it can compare new list with previous one and add notifications for those posts that not exist in later.
Your answer
Follow this Question
Related Questions
Best way to check internet connectivity (Android + iOS) 4 Answers
How to read text file (.txt) from URL? 3 Answers
yield return WWW stops Coroutine? 0 Answers
Google Insights 0 Answers
Problem with WWW.progress 3 Answers