【问题标题】:How to receive the data sent from client如何接收客户端发送的数据
【发布时间】:2020-03-06 15:05:12
【问题描述】:

我正在尝试使用 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
{}

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

【问题讨论】:

  • 如果您的服务器位于本地主机上,则使用 10.0.2.2 作为 BASE_URL:link
  • 是的,我使用 10.0.2.2 作为 BASE_URL 仍然无法检索数据。
  • link字段使用Expose和SerializedName
  • How to receive the data sent from server ???阅读您的帖子看起来好像您是:How to receive the data sent from client
  • 可以使用HttpLoggingInterceptor记录请求link

标签: android node.js express retrofit2


【解决方案1】:

我不在 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

【讨论】:

  • 我有一个问题,你能回答吗?我将它发布在堆栈溢出中,但它显示问题已关闭。
  • 先搜索解决方案,试一次。如果您没有找到任何解决方案,或者您无法自己解决,请在 StackOverflow 上使用正确的minimal reproducible example 提出一个新问题。如果不是我,其他 SO 用户会尝试帮助您。祝你好运! (如果问题太小,你可以在这里问,我会尽量给你答案的链接)
  • 我正在研究一个需要根据用户输入的输入生成图像的 android 应用程序创意。有没有图书馆可以提供帮助?并且可以在手机本身上生成图像,如果是,那么如何?或者它只能在服务器上生成,然后发送回客户端应用程序。我找到了一些使用 html canvas 来执行此操作的答案,但我认为当目标用户数量很大时,这种方法并不可取,因为它会速度慢、容易出错并且不可靠。那么如何才能牢记网络速度、用户数量、可靠性等因素呢?
  • 您要生成什么样的图像?比如签名什么的?
  • 像收据或账单或发票等这些类型的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2021-06-25
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多