我正在尝试在本地网络中建立我的Android应用和WampServer之间的通信。当我想从服务器读取数据时,已经成功了,但是当我尝试向服务器发送数据时出现问题。我使用服务来建立通信:
当我运行我的应用程序时,没有任何东西被添加到我的数据库中。我认为
请告诉我代码有什么问题?
public class SynchronisationService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://192.168.37.23/happiness_barometer/php_input.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(false);
connection.setRequestMethod("POST");
connection.connect();
OutputStream outputStream = connection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
JSONObject jsonObject = new JSONObject();
jsonObject.put("rate", 1);
writer.write(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
writer.flush();
writer.close();
} catch (Exception e) {
Log.v("EXCEPTION", e.getMessage());
}
}
}).start();
stopSelf();
return flags;
}
而我的PHP文件:
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=happiness_barometer;charset=utf8', 'utilisateur', '');
} catch (Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$sql = $bdd->prepare(
'INSERT INTO rates (rate, comment, category, day, month, year, hour, minute, day_of_week, week, rate_number)
VALUES (:rate, :comment, :category, :day, :month, :year, :hour, :minute, :day_of_week, :week, :rate_number)');
if (!empty($_POST['rate'])) {
$sql->execute(array(
'rate' => $_POST['rate'],
'comment' => '',
'category' => 'pro',
'day' => 19,
'month' => 8,
'year' => 2015,
'hour' => 18,
'minute' => 3,
'day_of_week' =>3,
'week' => 33,
'rate_number' => 2));
}
?>
当我运行我的应用程序时,没有任何东西被添加到我的数据库中。我认为
$_POST['rate']
中没有内容。请告诉我代码有什么问题?