我想向服务器发送多个sqlite记录

时间:2014-04-12 07:35:31

标签: android json

我希望在我的互联网连接回来时向服务器发送多条SQLite数据记录,我编写了一个broadcast receiver,当我的互联网回来时可以正常工作,但它只发送一个数据到服务器,我想在互联网回来时发送表的所有记录,建议我如何在for循环中将参数传递给async task,将每个数据传递给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
        }`

1 个答案:

答案 0 :(得分:1)

return null循环的右大括号之前有语句for。你的循环只运行一次并返回,这就解释了为什么只发送一条记录。