Swine1973
Swine1973

Reputation: 192

Java client and node.js server

im trying to implement a simple comet app to just send data and recive data, my client side is written on java and the server is in node.js, im trying to implement it from the client side with HttpUrlConnection but it seems that when i try to write to the server it doesnt respond me. so how can code the server to respond? (currently using http.createServer(function(req, res){...}).listen(8124);

Upvotes: 1

Views: 1743

Answers (2)

Boycott A.I.
Boycott A.I.

Reputation: 18871

Having used it (quite easily) in my own Android app, I recommend Socket.IO-client Java.

It's a "full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later."

Upvotes: 0

Stephen
Stephen

Reputation: 8178

I'm doing something similar to that with an Android app. I can't say that this is the "right" way to do it, but I'm using these classes in my project (to make POSTs and check the responses):

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

To implement I'm doing something to the effect of

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(/*...(String) url...*/);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key", "value"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost); // <-- this has useful info

To actually get useful stuff from the server I'm using JSON because, IMHO, XML parsing is a huge pain in Java.

So these are the classes I use for parsing the JSON that nodejs spits out at me.

import org.json.JSONArray;
import org.json.JSONObject;

...

JSONObject jObject = new JSONObject(EntityUtils.toString(response.getEntity()));
JSONArray datasets = jObject.getJSONArray("blahblahblah");

Upvotes: 1

Related Questions