我是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代码.....这里我要求从输入时间输入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');
?>
答案 0 :(得分:1)
将您的连接需求移至脚本顶部。
你在没有连接的情况下执行第一个查询,所以总是会失败,因此需要插入新行的ELSE
此外,您正在错误的位置运行INSERT,它应该在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);
}