0

我正在使用 mongodb 和 node.js 来构建一个 rest Api 顺便说一句我是初学者..所以我使用 express 启动了我的 localhost3000 但是当我在 android 中发布时它只给出 id 而不是内容..

喜欢

[{"_id":"59f4bb537ddb610f0c94fcd0","__v":0},
 {"_id":"59f4bca47ddb610f0c94fcd1","__v":0},
 {"_id":"59f4c5ce7ddb610f0c94fcd2","__v":0}
]

它只给出 localhost:3000/api/vehicles 中的 id

我的安卓主活动

    String url = "http://10.0.2.2:3000/api/vehicles";
   try{


  JSONObject jsonObject=new JSONObject();
  jsonObject.put("UserName" , "zaid");
  jsonObject.put("Password" , "passwords");
  final String mRequestBody = jsonObject.toString();

  JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, url, 
         jsonObject, new Response.Listener<JSONObject>() {
      @Override
      public void onResponse(JSONObject response) {
           System.out.println(response.toString());
      }
  }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {

      }
  }){
      @Override
      public String getBodyContentType() {
          return "application/json; charset=utf-8";
      }

      @Override
      public byte[] getBody() {
          try{
              return mRequestBody == null ? null : 
            mRequestBody.getBytes("utf-8");
          }catch (UnsupportedEncodingException e){
           return null;
          }
      }

      @Override
      protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

          if(response.statusCode == 200){

          }
          return super.parseNetworkResponse(response);
      }

  };

  Volley.newRequestQueue(this).add(jor);
      }
  catch (JSONException e){

    }

node.js

var mongoose = require('mongoose');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
Vehicle = require('./app/models/vehicle')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost:27017/codealong');
var port = 3005;

var router = express.Router();

app.use('/api', router);


router.use(function(req, res, next) {
    // body...
    console.log('Processing......');

    next();
});

router.get('/', function(req, res) {

    res.send('HELOOOOO ' + 'World');
    // body...
});
router.route('/vehicles')

    .post(function(req, res) {
        // body...
        var vehicle = new Vehicle();
        vehicle.make = req.body.make;
        vehicle.model = req.body.model;
        vehicle.color = req.body.color;

        vehicle.save(function(err) {
            if (err) {
                res.send(err);
            };

            res.json({ message: 'Vehicle as manufactured' });
            // body...
        });
    })

    .get(function(req, res) {
        Vehicle.find(function(err, vehicles) {
            if (err) {
                res.send(err);
            }
            res.json(vehicles);
            // body...
        });
        // body...
    });


router.route('/vehicles/:id')

    .get(function(req, res) {


        Vehicle.findById(req.params.id, function(err, vehicles) {
            if (err) {
                res.send(err);
            }
            res.send(vehicles);
            // body...
        }); // body...
    });

.delete(function(req, res) {
        // body...
        var id = req.params.id;
        Vehicle.removeV(id, function(err, vehicle) {
            if (err) {
                throw err;
            }
            res.json(vehicle);
            // body...
        });
    })

    .put(function(req, res) {
        var id = req.params.id;
        var veh = req.body;
        Vehicle.updateV(id, veh, {}, function(err, vehicles) {
            res.json(vehicles);
            // body...
        });
        // body...
    });


router.route('/vehicles/make/:make')

    .get(function(req, res) {

        var query = { make: req.params.make };
        Vehicle.find(query, function(err, vehicles) {

            if (err) {
                res.send(err);
            }
            res.json(vehicles);
            // body...
        });
        // body...
    });


///fire up //////////

app.listen(port);

console.log('server started');

所以我需要这是我在 js 中编辑的 app.js 文件

4

0 回答 0