Android客户端如何从Node.js服务器接收响应?

时间:2014-12-07 16:14:57

标签: android node.js

我想知道, Android 客户端如何才能收到Node.js服务器的响应或请求?我很清楚要使用 SocketIO 或默认 HttpRequest 向Node.js服务器发送请求,但我没有在网络上找到任何文档来接收请求或响应来自 Node.js 服务器。

如果您有任何解决方案或建议,请告诉我。

1 个答案:

答案 0 :(得分:0)

我写了一个关于将XML发送到Web服务并等待JSON响应的类似问题的答案:

/* 
* 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 
}    

这有帮助吗?