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

时间:2019-11-10 20:33:45

标签: android node.js express retrofit2

我正在尝试使用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发布数据的方法

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();
        }
    });
}

req.body中应该有一个json对象,该对象应该在终端中打印,但是会打印出来:-

Server Started at port 8000
{}

请帮助我检索服务器上的数据。

1 个答案:

答案 0 :(得分:0)

我不在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());

尝试一下!

body-parser doc