0

I want to send more than one record of SQLite data to server when my internet connection is come back, i have programmed a broadcast receiver which works when my internet is come back, but it send only one data to server, i want to send all the records of table when internet come back, suggest me that how to pass arguments to async task in for loop, an take each data to params.

public class BroadcastCreateTask extends BroadcastReceiver { DatabaseHandler db; public ProgressDialog pDialog; JSONParser jsonParser=new JSONParser(); private static String url_insert_task= ""; // JSON Node names private static final String TAG_SUCCESS = "success"; @Override public void onReceive(Context context, Intent intent) { db= new DatabaseHandler(context); boolean status = NetworkUtil.isNetworkAvailable(context); String s = String.valueOf(status); if(s.equals("true")) Toast.makeText(context, "Connected to internet", Toast.LENGTH_LONG).show(); new createTask().execute(); Toast.makeText(context, "data send to server", Toast.LENGTH_SHORT).show(); if(s.equals("false")) Toast.makeText(context, "Not Connected to internet", Toast.LENGTH_SHORT).show(); } class createTask extends AsyncTask<String, Void,String> { @Override protected String doInBackground(String... arg0) { List<Task> tasks = db.getAllContacts(); for(Task t : tasks){ String o = t.getOwner(); String s = t.getSubject(); String st = t.getStartDate(); String dt = t.getDueDate(); String c = t.getContacts(); String sta = t.getStatus(); String p = t.getPriority(); String d = t.getDescription(); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("owner",o)); params.add(new BasicNameValuePair("subject",s)); params.add(new BasicNameValuePair("startdate",st)); params.add(new BasicNameValuePair("duedate",dt)); params.add(new BasicNameValuePair("contacts",c)); params.add(new BasicNameValuePair("status",sta)); params.add(new BasicNameValuePair("priority",p)); params.add(new BasicNameValuePair("description",d)); JSONObject json = jsonParser.makeHttpRequest(url_insert_task, "POST", params); db.deleteContact(new Task(o,s)); // check log cat for response Log.d("Create Response", json.toString()); try { int success = json.getInt(TAG_SUCCESS); if(success==1) { pDialog.dismiss(); } } catch (JSONException e) { // TODO: handle exception e.printStackTrace(); } return null; } return null; } // end of background method @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); } } // end of async task }` 
8
  • You should just be able to do a query and loop over the rows in the Cursor returned from the query. Perhaps if you show your current implementation we could help you better. Commented Apr 12, 2014 at 7:37
  • i have written for loop in async task,but ultimately async task will be called only once, n how to take parameters to async task and set to params, i m not able to get it . Commented Apr 12, 2014 at 7:42
  • It is difficult to understand your description. Please show the code you have written to help me understand your problem. Commented Apr 12, 2014 at 7:53
  • see, in question, i have edited it, n posted code, i have to put for loop in OnReceive() method, n take all the sqlite data , n pass it to async task, but i dnt know how to make it happen, Commented Apr 12, 2014 at 7:57
  • It looks like you are making one web call for each row. Is that what you are supposed to do? Or are you supposed to make a JSONArray where each item is a database record and just make one web call for the entire data set? Commented Apr 12, 2014 at 8:01

1 Answer 1

1

You have the statement return null before the closing brace of the for loop. Your loop runs only once and returns, which explains why only one record is being sent.

Sign up to request clarification or add additional context in comments.

3 Comments

so what should i do .?
i removed that line, but same thing happen again, it send only one data, and then force close error occurs,
haha, u knw , i got exactly what i want, i used log.d in my broadcast, but i removed log.d, bcz broadcast receiver how will do it,, :D but after removing it, i got my result,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.