我正在尝试使用nodeJs和phpMyAdmin将从加速度计数据派生的值保存到数据库中。到现在为止它不起作用但不知道该怎么做。
这是我的主要活动
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d(TAG, "onSensorChanged: X: " + sensorEvent.values[0] + "Y: " + sensorEvent.values[1] + "Z: " + sensorEvent.values[2]);
xValue.setText("xValue: " + sensorEvent.values[0]);
yValue.setText("yValue: " + sensorEvent.values[1]);
zValue.setText("zValue: " + sensorEvent.values[2]);
double rf = Math.sqrt((sensorEvent.values[0]*sensorEvent.values[0]) + (sensorEvent.values[1]*sensorEvent.values[1]) + (sensorEvent.values[2]*sensorEvent.values[2]));
ravefactor.setText("ravefactor: "+ rf);
if (System.currentTimeMillis()-lastUpdate > 30000) {
sendRavemeter(rf);
lastUpdate = System.currentTimeMillis();
}
}
private void sendRavemeter(double rf) {
HttpRequest request = new HttpRequest();
try {
double lat = 8;
double lon = 9;
request.execute("/ravemeter/factor/" + rf + "/" + lat + "/" + lon, "POST").get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
这就是我在index.js
中的内容// Import and instantiate MySQL connector
const mysql = require('mysql');
const pool = mysql.createPool({
host: "iosense.tudelft.nl",
port: "3306",
user: "user95",
password: "123yWWjw",
database: "database95",
connectionLimit: 100,
multipleStatements: true,
debug: false
});
// Import web server library Express
const express = require('express');
const path = require('path');
// Define the web application with Express
const app = express();
// Add the ability to parse the request body (instead of the raw string)
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({limit: '50mb'}));
app.post('/ravemeter', (request, response) => {
const body = request.body;
// Check the body contains the player name
if (body.lat !== undefined && body.lon !== undefined && body.ravefactor !== undefined) {
//var computationResult = calculateSth(body.lat, body.lon, body.ravefactor);
// The ? will be replace by all the parameters of player
const sql = 'INSERT INTO `TableRaveApp` SET ?';
// Create a player object with all parameters
const ravemeterData = {
lat: body.lat,
lon: body.lon,
ravefactor: body.ravefactor
};
// Send the query and the data (player) to be executed
execSQL(sql, ravemeterData).then( (result) => {
response.send("ravemeter activated");
}).catch( (error) => {
response.send(error);
});
} else {
response.send({error: "Missing gps and/or ravefactor."});
}
});
app.post('/ravemeter/factor/:ravefactor/:lat/:lon', (request, response) => {
const ravefactor = request.query.ravefactor;
const sql = 'INSERT INTO `TableRaveApp` SET ?';
// Create a player object with all parameters
const ravemeterData = {
lat: request.query.lat,
lon: request.query.lon,
ravefactor: request.query.ravefactor
};
// Send the query and the data (player) to be executed
execSQL(sql, ravemeterData).then( (result) => {
response.send("ravemeter activated");
}).catch( (error) => {
response.send(error);
});
});
app.get('/ravemeter/factor/:ravefactor', (request, response) => {
const rave = request.query.ravefactor;
const sql = 'SELECT COUNT(*) AS \'num_persons\', AVG(`ravefactor`) AS \'avg_ravefactor\' FROM `TableRaveApp`' ;
// Send the query (without data) to be executed
execSQL(sql, rave).then( (result) => {
response.send({ravefactor_values: result});
}).catch( (error) => {
response.send(error);
});
});
app.get('/ravemeter/area/:area', (request, response) => {
const area = request.query.area;
const LatLonArea = getLatLonArea(area);
const sql = 'SELECT COUNT(*) AS \'num_persons\', AVG(`ravefactor`) AS \'avg_ravefactor\' FROM `TableRaveApp` WHERE lat BETWEEN ? AND ? AND lon BETWEEN ? AND ? ';
// Send the query (without data) to be executed
execSQL(sql, latLonArea).then( (result) => {
response.send({ravemeter_values: result});
}).catch( (error) => {
response.send(error);
});
});
function getLatLonArea(areaName) {
switch(areaName) {
case 'mainStage': return [6568,56675,7634,3948];
case 'bravoStage': return [6568,56675,7634,3948];
case 'technoStage': return [3245, 5469, 8795, 5430];
case 'foodArea': return [4239, 5438, 5432, 2349];
}
}
/**
* Execute an SQL query.
**/
function execSQL(sql, data) {
// Build a promise (result via then(), error via catch())
return new Promise((resolve, reject) => {
pool.getConnection((error, connection) => {
if (error) {
// Write a log to keep track of errors
console.error(error);
// Reject the promise, reject will trigger the catch()
reject(error);
}
const query = connection.query(sql, data, (error, result) => {
connection.release();
if (error) {
if (error.errno == 1062) { // MySQL error for duplicated entries
reject({error: "Already exist.", errno: 500});
} else {
// Write a log to keep track of errors
console.error(error);
// Reject the promise, reject will trigger the catch()
reject({error: 'Something wrong happened :(', errno: 500});
}
}
// Result from the database, resolve will trigger then()
resolve(result);
});
});
});
}
// Start the web server
app.listen(8081, 'localhost');
console.info('Running on http://localhost:8081');
我不知道如何解决它因为它没有给出任何错误所以不能谷歌它。我也发了一个http请求 有人知道该怎么办??
谢谢!! This is a screenshot of what I actually want to do, but now I do it manually with an extension. I want to post it to the database from the Mainactivity with the request.execute