我是android的新手。这里我让简单的service.which在5分钟后自动刷新。但问题是数据库每次都会重复上传。所以帮帮我..。而且在我的代码中,每次都会上传单个字符串,但我不想做JSONArray。所以请帮我解决如何在我的代码中制作JSONArray .
public class One extends Service {
    public static final long NOTIFY_INTERVAL = 5*60*1000;
    private Handler handler = new Handler();
    private Timer timer = null;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        if(timer != null)
        {
            timer.cancel();
        }
        else
        {
            timer = new Timer();
        }
        timer.scheduleAtFixedRate(new DisplayTime(),0,NOTIFY_INTERVAL);
    }
    class DisplayTime extends TimerTask
    {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    recll();
                    Toast.makeText(getApplicationContext(),"run",Toast.LENGTH_SHORT).show();
                }
            });
        }
        private void recll() {
            Uri uri = Uri.parse("content://sms/inbox");
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
                    final String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
                    final String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
                    Date date1 = new Date(Long.valueOf(date));
                    final String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString();
                    final String fDate = date1.toString();
                    cursor.moveToNext();
                    class getSMSDetails extends AsyncTask<Void,Void,String>
                    {
                        @Override
                        protected String doInBackground(Void... params)
                        {
                            HashMap<String,String> param = new HashMap<String, String>();
                            param.put(Connect.KEY_NUMBER,number);
                            param.put(Connect.KEY_TYPE,type);
                            param.put(Connect.KEY_DATE,fDate);
                            param.put(Connect.KEY_BODY,body);
                            RequestHandler rh = new RequestHandler();
                            String res = rh.sendPostRequest(Connect.URL_ADD, param);
                            return res;
                        }
                    }
                    getSMSDetails idata = new getSMSDetails();
                    idata.execute();
                }
            }
            cursor.close();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}这是我的PHP code.....here,我要求从输入时间开始输入7天的数据,每当新的条目出现时,我想输入新的条目,而它再次输入整个数据,所以我想防止重复输入,谢谢预先.
<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
   //Getting values
    $number = $_POST['number'];
    $type = $_POST['type'];
    $dates = $_POST['date'];
    $content = $_POST['content'];
    $start = strtotime("-7 days");
    $date = strtotime($dates);
    $end = strtotime("now");
    $query=mysqli_query($con,"SELECT * FROM message_detail where number =   '$number' and type = '$type' and date = '$date' and content = '$content'");
    if(mysqli_num_rows($query)>0) 
    {
        echo "already exist";
    }
    elseif($start <= $date &&  $end >= $date)
    {
        //Creating an sql query
        $sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES   ('$number','$type','$date','$content')";
    } 
    //Importing our db connection script
    require_once('connect.php');
    //Executing query to database
    if(mysqli_query($con,$sql)){
        echo 'Entry Added Successfully';
    }else{
        echo 'Could Not Add Entry';
    }
    //Closing the database 
    mysqli_close($con);
}connection.php
<?php
//Defining Constants
define('HOST','mysql.hostinger.in');
define('USER','u336100496_hiren');
define('PASS','');
define('DB','u336100496_sebu');
//Connecting to Database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>发布于 2016-09-06 10:30:50
将连接要求移动到脚本的顶部。
如果是在没有连接的情况下执行第一个查询,那么它总是会失败,因此接受插入新行的ELSE。
另外,您在错误的位置运行插入,它应该在ELSEIF中。
<?php 
//Importing our db connection script
require_once('connect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
   //Getting values
    $number = $_POST['number'];
    $type = $_POST['type'];
    $dates = $_POST['date'];
    $content = $_POST['content'];
    $start = strtotime("-7 days");
    $date = strtotime($dates);
    $end = strtotime("now");
    $query=mysqli_query($con,"SELECT * 
                              FROM message_detail 
                              where number = '$number' 
                                and type = '$type' 
                                and date = '$date' 
                                and content = '$content'");
    if(mysqli_num_rows($query)>0) {
        echo "already exist";
    }
    elseif($start <= $date &&  $end >= $date) {
        //Creating an sql query
        $sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES   ('$number','$type','$date','$content')";
        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Entry Added Successfully';
        }else{
            echo 'Could Not Add Entry';
        }
    } 
    //Closing the database 
    mysqli_close($con);
}https://stackoverflow.com/questions/39345931
复制相似问题