简体   繁体   English

将字符串数组从java客户端发送到NodeJs服务器

[英]Send string array from java client to NodeJs server

I'm developing an Android app that will send a string array to an external NodeJs server running on the Raspberry Pi. 我正在开发一个Android应用程序,它将一个字符串数组发送到在Raspberry Pi上运行的外部NodeJs服务器。

I'm currently handling the sending side of the app using an AsyncTask, with a socket layer setup inside targeted at the NodeJs server. 我目前正在使用AsyncTask处理应用程序的发送端,其中针对NodeJs服务器设置了套接字层。 The server is successfully receiving calls from the app using net and socket modules, however I'm struggling to pass actual data. 服务器使用网络和套接字模块成功接收来自应用程序的调用,但是我很难传递实际数据。

At the moment I'm using OutputStream to send data. 目前我正在使用OutputStream发送数据。

OutputStream socketStream = socket.getOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(socketStream);
objectOutput.writeObject(new String[] {"Test", "Test2", "Test3"});


Log.i("Array Object", objectOutput.toString());

objectOutput.close();
socketStream.close();

Is this the correct method of writing a string array to send to a NodeJs server? 这是编写字符串数组以发送到NodeJs服务器的正确方法吗?

Also what would the code contain on the NodeJs server in order to read this written data once it has been received? 此外,代码在NodeJs服务器上包含什么才能在收到后读取这些写入的数据?

Update 更新

The NodeJs server is based off this tutorial http://helloraspberrypi.blogspot.co.uk/2014/03/create-tcp-server-using-nodejs-with-net.html NodeJs服务器基于本教程http://helloraspberrypi.blogspot.co.uk/2014/03/create-tcp-server-using-nodejs-with-net.html

Where the function callback_server_connection is called in the net.createServer(callback_server_connection); 在net.createServer(callback_server_connection)中调用函数callback_server_connection的地方; function. 功能。

function callback_server_connection(socket){
    var remoteAddress = socket.remoteAddress;
    var remotePort = socket.remotePort;
    socket.setNoDelay(true);
    console.log("connected: ", remoteAddress, " : ", remotePort);

    var msg = 'Hello ' + remoteAddress + ' : ' +  remotePort + '\r\n'
        + "You are #" + count + '\r\n';
    count++;

    socket.end(msg);

    socket.on('data', function (data) {
        console.log(data.toString());
    });

    socket.on('end', function () {
        console.log("ended: ", remoteAddress, " : ", remotePort);
    });
  }

尝试使用json序列化您的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM