0

I was wondering , how can an Android client can receive a response or request from Node.js server? I am quite clear about to send a request to Node.js server using SocketIO or default HttpRequest but i am not finding any documentation in the web to receive a request or response from Node.js server.

Please let me know , if you have any solution or suggestion.

1

1 Answer 1

0

I had written an answer for a similar question about sending an XML over to a web service and waiting for a JSON response:

/* 
* Getting the XML ready for sending 
*/ 
String xml = getXml(); // YOUR XML STRING  
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add( new BasicNameValuePair("XML",xml) );
/* 
* Getting ready to send and receive from server 
*/ 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("/your/service/url");
post.setEntity(new UrlEncodedFromEntity(nvps));
post.addHeader("Content-Type","application/xml");
post.addHeader("Accept","application/json");
/* 
* Sending the file and waiting for response 
*/ 
HttpResponse response = httpClient.execute(post);
/* 
* Getting the JSON from the response 
*/ 
if( response.getStatusLine().getStatusCode() == 200 ){
    String json = EntityUtils.toString(response.getEntity());
    // PROCESS RESPONSE HERE 
}    

Does this help?

4
  • Yes thanks but how can i do the same thing using sockets? I mean using SocketIO
    – MyCode
    Dec 7, 2014 at 16:28
  • Wait, what language are you coding in? JavaScript? My solution is in Java
    – An SO User
    Dec 7, 2014 at 16:37
  • Client Side : Android (JAVA) , Server Side : Node.js (JavaScript)
    – MyCode
    Dec 7, 2014 at 16:45
  • Yes, so my solution is valid :) I posted client-side.
    – An SO User
    Dec 7, 2014 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.