我想知道是否可以立即接收從服務器(Servlet)發送到客戶端(Andriod)的消息,並且客戶端可以注意到該消息並立即迴應它?立即接收從服務器發送的消息(Servlet到android)
感謝您的幫助!
我想知道是否可以立即接收從服務器(Servlet)發送到客戶端(Andriod)的消息,並且客戶端可以注意到該消息並立即迴應它?立即接收從服務器發送的消息(Servlet到android)
感謝您的幫助!
您可以。
HAVE A LOOK AT: http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
你的問題沒有足夠的細節很好的教程。但我想你想問: 由於你的應用程序安裝並運行在設備上,你的服務器發送一條消息。 由於您的設備收到消息,它會發送響應。
是的,這是可能的。隨着應用程序的安裝和運行,您應該使用onCreate或onStart方法調用您的servlet。隨着您的服務器消息收到,您將再次從您的設備發送消息。 段:
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.yourserver/servlet";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
//or call another url hit with your message
}
和第二請求響應上接收是
HttpClient client = new DefaultHttpClient();
String postURL = "http://yourserver";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("message", "your message"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
--------------------------編輯---------------
對不清楚的問題。我的意思是這條消息是從服務器端發送的,並且是由服務器創建的,例如android設備通過servlet接收來自其他用戶的消息 – user1244556 2012-03-02 08:29:23