如何接收客户端发送过来的数据

How to receive the data sent from client

我正在尝试使用 retrofit2 库从 android 应用程序发送数据(POST 请求)并在用 nodejs(使用 express 框架)编写的服务器上接收它,但我无法检索从应用程序发送的数据。

我使用 GsonConverterfactory 进行改造,并向“/temp”路由发送了一个 POST 请求。

Index.js(处理路由请求):-

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.urlencoded({extended:true}));
app.use('/public',express.static('public'));

app.post("/temp",function(req,res){
    console.log(req.body);
    obj = {
        orgName : "Got the message ",
        address : "on the server"
    }
    res.json(obj);
})

app.listen(8000,function(){
    console.log("Server Started at port 8000");
})

Shop.java

package com.example.myapplication;
public class Shop {
    private String orgName;
    private String address;

    public Shop(String orgName, String address) {
        this.orgName = orgName;
        this.address = address;
    }

    public String getOrgName() {
        return orgName;
    }  

    public String getAddress() {
        return address;
    }
}

ShopApi.java

package com.example.myapplication;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface ShopApi {
    @POST("temp")
    Call<Shop> create(@Body Shop shop);
}

postData() - 从 MainActivity.java

获取 post 数据的方法
public void postData(View view){
    String BASE_URL = "http://10.0.2.2:8000/";
    String org = orgName.getText().toString();
    String address = add.getText().toString();
    //Toast.makeText(getApplicationContext(),org+" "+address,Toast.LENGTH_SHORT).show();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ShopApi shopApi = retrofit.create(ShopApi.class);
    Shop shop = new Shop(org,address);
    Call<Shop> shopCall = shopApi.create(shop);
    shopCall.enqueue(new Callback<Shop>() {
        @Override
        public void onResponse(Call<Shop> call, Response<Shop> response) {
            if(!response.isSuccessful()){
                Toast.makeText(getApplicationContext(),response.code(),Toast.LENGTH_LONG).show();
            }
            Shop shopResponse = response.body();
            String content = shopResponse.getOrgName() + shopResponse.getAddress();
            Toast.makeText(getApplicationContext(),content,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<Shop> call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });
}

json 对象应该在 req.body 中,它应该在终端中打印出来,但是它被打印出来了 :-

Server Started at port 8000
{}

请帮我取回服务器上的数据。

我不在 Node.js 工作,但我还是试着回答你的问题。

您希望服务器中有一个如下所示的 JSON 对象,

{
   "orgName": "Organization name",
   "address": "Organization address"
}

好的,您的 Android 部分(Retrofit api 界面)是正确的。在 运行 时间内,它会生成预期的 JSON 对象。但是,在您的服务器端,您接受的是 application/x-www-form-urlencoded 而不是 application/json 数据。这是导致此问题的原因 IMO。

只需替换

中的以下代码
app.use(bodyParser.urlencoded({extended:true}));

app.use(bodyParser.json());

并尝试一次!

正文解析器 doc