2014-12-07 125 views
0

我想知道,Android客戶端如何能夠接收來自Node.js服務器的響應或請求?我很清楚要發送一個請求到Node.js服務器使用SocketIO或默認HttpRequest但我沒有在網絡上找到任何文檔從接收請求或響應Node.js服務器。Android客戶端如何從Node.js服務器接收響應?

請讓我知道,如果你有任何解決方案或建議。

+0

我覺得這個鏈接可能有幫助:http://socket.io/blog/native-socket-io-and-android/ – ahitt6345 2016-08-12 16:40:45

回答

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 
}  

這是否幫助?

+0

是的,謝謝,但我怎麼能用套接字來做同樣的事情?我的意思是使用SocketIO – MyCode 2014-12-07 16:28:18

+0

等一下,你在用什麼語言編碼? JavaScript的?我的解決方案是Java – 2014-12-07 16:37:04

+0

客戶端:Android(JAVA),服務器端:Node.js(JavaScript) – MyCode 2014-12-07 16:45:30

相關問題