Continuously Pull Data from Backend Database?

时间:2016-03-04 18:03:47

标签: java android android-fragments android-asynctask

I am new to Android development and Java, so bear with me, and please let me know if any of my phrasing or anything is wrong. I've searched for many hours, and I've come across so many unknown terms and methods of implementation that may or may not work that I've gone and thrown myself into a tizzy.

I'm currently developing a type of feed for an Android app, and my main goal is that I want to be able to pull down on the feed (similar to the Gmail app) and have newly-entered data in the database loaded onto the device and displayed on the screen. Just to be clear, I only want to pull data when the app is opened.

Here's some background on the app. The main activity hosts an inner FragmentPagerAdapter class which controls 3 fragments in a TabLayout. The first tab is the feed, the second is the list of contacts (which I also want to update in a manner similar to the feed), and the third is just profile stuff.

What I've done so far is that I have a seeded db on a backend server (Python/SqlAlchemy), and I've set up an AsyncTask in both the feed and contacts fragments that GETs (using HttpURLConnection) all the appropriate data and brings it client-side. From there I extract the data I need from the received JSON, and display it in dynamically generated TextViews. This part all works great.

I've tried stopping/restarting the AsyncTask, but the docs say that's a no-go. I've tried the notifyDataSetChange(), but it's also not working. Also, just to confirm, does notifyDataSetChange() automatically update the fragments in a way that accomplishes my goal? I've read some of the documentation about it and looked at many topics here, but I couldn't put together a clear answer for my implementation and I'm really confused about it...

So can someone can direct me in where to learn how to continuously pull data from a backend database after the AsyncTask has already been run or is currently running? Or at least how to phrase it so I can search more efficiently? Thanks!

1 个答案:

答案 0 :(得分:0)

type mismatching found List[Any], required List[Integer] 是要走的路。在AsyncTask中,执行onResume( )。是的,文档确实说重复使用AsyncTask是不行的。所以你需要这样做:

AsyncTask

现在那是一次性更新。如何定期更新?最简单的方法是使用定期触发new MyAsyncTask.execute( url ); 的{​​{1}}。文档:http://developer.android.com/reference/android/os/CountDownTimer.html

此外,不要制作匿名CountDownTimer,而是制作一个扩展它的类。所以,你会有一个名为AsyncTask的类。 CountDownTimer是你MyCountDownTimer extends CountDownTimer解雇的地方。

希望有所帮助