1

I'm trying to establish connection between an android studio and a node js server, but I'm kinda stuck. I pretty sure I've done many things wrong, but my one error message I can start searching for solution from is a listen EADDRINUSE :::3000 error on the Node server side. Here are all the codes from both sides, I really hope some more experienced eyes can point out the issue and give me a direction to continue at. Thanks in advance!

Node JS code:

var express = require('express');
var app = express();

app.on('listening',function(){
    console.log('ok, server is running');
});

app.post('/postdata', (req, res) => {
    var data = req.body.data; // your data
    // do something with that data (write to a DB, for instance)
    console.log(data)
    res.status(200).json({
        message: "Data received successfully"
    });
});
app.listen(3000);

Android Studio code(MainActivity.java):

package com.example.tbg_studio;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    Button RequestButton; // button which on clicking, sends the request
    TextView DisplayText; // a text field to display the request response
    EditText DataField; // a text field where the data to be sent is entered

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestButton = (Button) findViewById(R.id.RequestButton);
        DataField = (EditText) findViewById(R.id.DataField);
        DisplayText = (TextView) findViewById(R.id.DisplayText);

        final RequestQueue queue = Volley.newRequestQueue(this);
        final String url = "http://serverdomainorip/postdata"; // your URL

        queue.start();
        RequestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> params = new HashMap<String,String>();
                params.put("data", DataField.getText().toString()); // the entered data as the body.

                JsonObjectRequest jsObjRequest = new
                        JsonObjectRequest(Request.Method.POST,
                        url,
                        new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    DisplayText.setText(response.getString("message"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        DisplayText.setText("That didn't work!");
                    }
                });
                queue.add(jsObjRequest);
            }
        });
    }

}

and the Node JS error message:

Error: listen EADDRINUSE :::3000
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1367:14)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1492:7)
at Function.listen (/home/leventecsoba/node_modules/express/lib/application.js:618:24)
at Object. (/home/leventecsoba/Asztal/Node JS/tbg_studio.js:16:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)

leventecsoba
  • 91
  • 1
  • 8

2 Answers2

0

It looks like you already have something (probably another instance of a Node server) running at :3000, so the code itself does not really matter. If killing processes does not make it for you, simply restart your computer (I know, it sounds silly, but it usually works!). With a fresh start, you'll be able to take control of port 3000 again and run the Node server you want there.

Alternatively, you change this line in your Node server to have it listen for requests on port 3001:

app.listen(3001)

You'll have to also update the code in Android Studio to have your mobile app send requests to port 3001.

Yves Gurcan
  • 553
  • 1
  • 5
  • 19
  • OK, sound good :) Quick question, the update needed in the android studio code should be in the `final String url = "http://serverdomainorip/postdata";` line, right? So like I sould change it to http://3001/postdata? Just to be sure :) – leventecsoba Dec 21 '19 at 19:41
  • Unfortunately, I don't know enough about Android Studio and Java to answer your question with confidence but from what I understand your URL would look something like this: `http://serverdomainorip:3001/postdata` or try that if you want to make sure it hits `localhost`: `http://192.168.1.1:3001/postdata`. If this works for you, don't forget to select this answer as valid for your question :) – Yves Gurcan Dec 21 '19 at 19:53
  • Didn't work, but thanks for the effort, I appreciate it! – leventecsoba Dec 21 '19 at 20:45
0

On your url pass the port where you want to connect.

  final String url = "http://serverdomainorip:3000/postdata";
AllanRibas
  • 339
  • 3
  • 10