'Send the data of HashMap from android to node.js using retrofit

I know I am doing something wrong but what exactly is the problem I don't know can someone please guide me through this

I'm sending data from android studio using Hashmap to my node code but unfortunately I am not getting that data in node server

My code in android studio :

public class MainActivity extends AppCompatActivity {
private RetrofitInterface retrofitInterface;
private Retrofit retrofit;
private String BaseURl = "http://10.0.2.2:3000";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    retrofit = new Retrofit.Builder().baseUrl(BaseURl).addConverterFactory(GsonConverterFactory.create()).build();
    retrofitInterface = retrofit.create(RetrofitInterface.class);
    HashMap<String, String> map = new HashMap<>();
    map.put("name", "meet Davda");
    retrofitInterface.executemessage(map);
    Intent i = new Intent(this, LoginAdmin.class);
    startActivity(i);
    finish();
}

and my nodeJs code :

const express = require('express')
const app = express()
app.use(express.json())
app.post('/message', (req, res) => {
var myname = req.body.name
console.log("the name is: " + myname)
})
app.listen(3000, () => {
console.log("listening on port 3000")
})

And my retrofit interface :

public interface RetrofitInterface {
@POST("/message")
Call<Void>executemessage(@Body HashMap<String,String>map);
}


Solution 1:[1]

I solved my issue with adding following line to nodejs code :

app.use(express.json());

My updated code would look like :

const express = require('express');
const app = express();
app.use(express.json());
app.post("/api/courses", (req, res) => {
myname = req.body.name
console.log("The name is: " + myname);
res.send(myname);
})
 app.get("/api/courses", (req, res) => {
 res.send(myname);
 })
 app.listen(3000, () => {
 console.log("listening to port 3000");
 });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Meet Davda