从Android Studio向Node.js发送请求以将数据保存在数据库中

时间:2019-08-07 07:25:31

标签: android node.js database android-studio express

我正在开发一个程序,该程序应包含2个部分。第一部分是制作一个向我展示位置和imei的android应用。我已经做到了。现在第二部分是将这些数据保存到Firebase / Mongodb数据库中,但使用对nodeje的后请求,而不仅仅是从android应用程序中保存。我有一些理解上的问题。我已经完成了android部分和服务器端。但是我不明白如何连接它们以及如何将这3个参数(纬度,经度和imei)与发布请求一起保存到数据库中。

这是Android活动

public class MainActivity extends AppCompatActivity {

private FusedLocationProviderClient client;
private TextView imei;

 public double latitude;
 public double longitude;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imei = findViewById(R.id.imei);
    loadIMEI();







    client = LocationServices.getFusedLocationProviderClient(this);
    Button button = findViewById(R.id.Chk);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, 
Manifest.permission.ACCESS_FINE_LOCATION) != 
PackageManager.PERMISSION_GRANTED && 
ActivityCompat.checkSelfPermission(MainActivity.this, 
Manifest.permission.ACCESS_COARSE_LOCATION) != 
PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then 
overriding
                //   public void onRequestPermissionsResult(int 
requestCode, String[] permissions,
                     //                                          int[] 
grantResults)
                // to handle the case where the user grants the 
permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            client.getLastLocation().addOnSuccessListener(new 
OnSuccessListener<Location>() {
                  @Override
                public void onSuccess(Location location) {
                    if (location!=null){
                        TextView textView = findViewById(R.id.textView);

                        longitude = location.getLongitude();
                         latitude = location.getLatitude();







  textView.setText(String.valueOf("latitudine"+longitude+ 
 "\n"+"longitutine"+latitude));

                    }

                }
            });



        }

    });

}

public void loadIMEI() {
    // Check if the READ_PHONE_STATE permission is already available.
    if (ActivityCompat.checkSelfPermission(this, 
Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {
//                get_imei_data();
        } else {
            ActivityCompat.requestPermissions(this, new String[] 
{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        }
    } else {

        TelephonyManager mngr = 
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);//
        IMEI = mngr.getDeviceId();

        imei.setText(mngr.getDeviceId());
        // READ_PHONE_STATE permission is already been granted.


    }
}


private String device_unique_id;

private static String IMEI;
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;


}

这是server.js

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');


//connect to mongodb:

mongoose.connect('mongodb+srv://tudorstanciulescu:19970826@test- 
srqul.mongodb.net/test?retryWrites=true&w=majority', {useNewUrlParser: 
true} );

var app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());


//route:

app.use('/api', require('./routes/api'));

//start server:

app.listen(3000);

console.log('serverul porneste pe port 3000');

这是api.js

//dependencies

var express = require('express');
var router = express.Router();

//models

var Status = require('../models/status');

//routes

Status.methods(['get', 'post', 'put']);
Status.register(router, '/status');

//return router

module.exports = router;

这是status.js

var restful = require('node-restful');
var mongoose = restful.mongoose;

var statusSchema = new mongoose.Schema({
latitudine: String,
longitudine: String,
imei: String
});

//return models

module.exports = restful.model('tblstatus', statusSchema);

我希望在数据库中看到具有imei,long和lat的结构。

0 个答案:

没有答案