java - Fetch JSON Data from URL and Repeatedly Update SQLite Database -
in app, create sqlite database. populate json data fetched url using instance of httpasynctask class in main activity. works fine, want update database. new data (one row in database) added url page once per day, , want implement "synchronize" button in app updates database new information. advice on how this? httpasynctask below, if helps - i'm thinking might need if/else clause in onpostexecute() method adds rows if database getting created first time. thought trying put httpasynctask class in databasehelper, doesn't make sense.
private class httpasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string...urls) { return get(urls[0]); } @override protected void onpostexecute(string result) { try { jsonobject main = new jsonobject(result); jsonobject body = main.getjsonobject("body"); jsonarray measuregrps = body.getjsonarray("measuregrps"); // measurements date, unit, , value (weight) (int = 0; < measuregrps.length(); i++) { jsonobject row = measuregrps.getjsonobject(i); // lot of getting & parsing data happens db.addentry(new entry(date, weight, null, null)); //adds lines every time run, want add //the lines once , add new rows 1 one there } } catch (jsonexception e) { e.printstacktrace(); } } } public static string get(string url) { inputstream = null; string result = ""; try { httpclient client = new defaulthttpclient(); httpget = new httpget(url); httpresponse httpresponse = client.execute(get); = httpresponse.getentity().getcontent(); if (is != null) result = convertinputstream(is); else result = "did not work!"; } catch (exception e) { log.d("input stream", e.getlocalizedmessage()); } return result; } private static string convertinputstream(inputstream is) throws ioexception { stringbuilder builder = new stringbuilder(); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); string line = ""; while((line = reader.readline()) != null) builder.append(line); is.close(); return builder.tostring(); }
your implementation totally depends on project requirements. if there continuously changes on server, right way implement synchronization process is:
1. implement sync process, works totally in background. sync customized call specific api calls/service classes required sync.
2. server prompt mobile client data change.
3. server updates, continuously running service/sync @ predefined intervals run , checks updates or implements gcm.
sync adapter best sync services. ohh, don't forget apply content provider, database calls concurrent ui , background both.
hope may decide.
Comments
Post a Comment