0

I'm developing an Android app that will send a string array to an external NodeJs server running on the Raspberry Pi.

I'm currently handling the sending side of the app using an AsyncTask, with a socket layer setup inside targeted at the NodeJs server. 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 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?

Also what would the code contain on the NodeJs server in order to read this written data once it has been received?

Update

The NodeJs server is based off this tutorial 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); 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);
    });
  }
robert
  • 23
  • 6
  • If these are your first tiries then why start with a string array? Just start with one string first and see what the server receives. – greenapps Feb 20 '15 at 14:32
  • If I'm honest, I'm very new to this, so am unsure as to how send/receive information – robert Feb 20 '15 at 14:34
  • That i understand. That's why I suggested you to start with sending one String first. – greenapps Feb 20 '15 at 14:35
  • This is the code to send one string: `String text = "my first message comes here."; OutputStream os = socket.getOutputStream(); os.write((text + "\n").getBytes());`. – greenapps Feb 20 '15 at 14:46
  • Ok thanks for this - the server side code is successfully displaying this string. Is it possible to send Key arrays with this output stream or can it only be done via strings? – robert Feb 20 '15 at 14:59
  • Probably. Don't know. But why would you? Why not send a line for every array item? Just put that code in a loop. `String myStrings[] = new String[] {"Test", "Test2", "Test3"};` then loop with: `os.write((myStrings[nr] + "\n").getBytes()); ` – greenapps Feb 20 '15 at 15:03
  • `Key arrays`. Please explain what you have in mind. – greenapps Feb 20 '15 at 15:08
  • I'd like to send variable notification information to the server. So for example ('packageName'=>varPackage, 'title'=>varTitle, 'info'=>varInfo) – robert Feb 20 '15 at 15:21
  • Well just send as strings "packageName=org.example.com" and so on. – greenapps Feb 20 '15 at 15:24

1 Answers1

0

Try to serialize your data with json

Yahya
  • 501
  • 4
  • 8