In my application I will get large amount of data from the server. I want to sync all the data by automatically with the device. Even when my app is not in use, I want to sync the data.
Right now I have done that by using AsyncTask. But it is taking long time to get the data.
This is the code I have written in my MainActivity.
public class UpdateTask extends AsyncTask<Void, Void, Void> {
private Context mCon;
public UpdateTask(Context con)
{
mCon = con;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... nope) {
try {
// Set a time to simulate a long update process.
//Thread.sleep(4000);
getData();
getProjectInit();
getProjRequirements();
return null;
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(Void nope) {
}
public void getProjRequirements()
{
projList = Project.listAll();
for(int i = 0;i<projList.size();i++)
{
proj = projList.get(i);
RequestParams params2 = new RequestParams();
AsyncClientHandler.get("projectMaster/Getdetails/"+proj.p_id, params2, new AsyncHttpResponseHandler()
{
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable error, String content) {
// TODO Auto-generated method stub
super.onFailure(statusCode, headers, error, content);
}
@SuppressWarnings("deprecation")
@Override
public void onSuccess(int statusCode, Header[] headers,
String content) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, content);
//Log.i("projectid",proj.p_id.toString());
try
{
JSONObject data = new JSONObject(content);
Log.i("projreq_content",content.toString());
JSONArray projectrequirement = data.getJSONArray("requirements");
Log.i("requirement",projectrequirement.toString());
for(int i=0;i<projectrequirement.length();i++)
{
JSONObject projrequire = projectrequirement.getJSONObject(i);
Log.i("projreqwqwq",projrequire.toString());
prrq = ProjectRequirements.getProjectRequirementsByPidRid(getValue(projrequire,"id"),getValue(projrequire,"pid"),getValue(projrequire,"sid"));
if(prrq==null)
prrq=new ProjectRequirements();
prrq.pr_id=getValue(projrequire,"id");
prrq.pid=getValue(projrequire,"pid");
prrq.srid=getValue(projrequire,"srid");
prrq.sid=getValue(projrequire,"sid");
prrq.rid=getValue(projrequire,"rid");
prrq.estimated_value=getValue(projrequire,"estimated_value");
prrq.expected_close_date=getValue(projrequire, "expected_close_date");
prrq.others=getValue(projrequire,"others");
prrq.requirement_id=getValue(projrequire,"requirement_id");
prrq.name=getValue(projrequire,"name");
prrq.save();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
}
I have given the getProjectRequirements() code.
But this will take long time to display it in other fragments.
For this scenario what can i prefer and could someone give me a example or any tutorial?
I have scene SyncAdapter in android developer website. But it's looking quite difficult for me.
Could someone provide me some easy example?
Thanks in advance!