i have two applications, i have the android app and the Ruby on Rails API. In android i have a SQLite database and almost all the time i need sync the android database with the API database, but this synchronization can take a "long time", something like 10 seconds if is the first sync, so user need to keep waiting and looking to load screen until the process done.
So, i want send a post to the Ruby on Rails application, but without "stop" the application in the load screen, i want to do this sync in background, so the user wont realise that the app is syncing with the API.
Now, i'm trying to working with threads, but it still fails.
Thanks.
In the activity you are working make a inner class, like this
class ExecuteTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
JSONObject jsonObject=new JSONObject();
url = "http://localhost:8080/abc/xyz";//your url
try {
jsonObject.put("id",params[0]);
jsonObject.put("password",params[1]);
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(jsonObject.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
HttpResponse response = httpclient.execute(post);
inputStream = response.getEntity().getContent();//final response
} catch (JSONException e) {
e.printStackTrace();
}
String res = CommomUtilites.post(url,jsonObject);
return res;
}
}
now in where you want to perform background task call write
new ExecuteTask.execute(String...params)
this will implicitly invoke call to
protected String doInBackground(String... params)
and params will be params which you passed in execute method.
This will work, Write comment if you face any problem.
Happy coding!!!