【问题标题】:Immediately receive message send from server(Servlet to android)立即接收从服务器发送的消息(Servlet 到 android)
【发布时间】:2012-03-02 07:44:00
【问题描述】:

我想知道是否可以立即接收从服务器(Servlet)发送到客户端(Andriod)的消息,并且客户端可以注意到该消息并立即响应它?

感谢您的帮助!!

【问题讨论】:

    标签: android servlets


    【解决方案1】:

    是的,

    推送通知是你的路要走..

    Google 提供 C2DM 作为 android 的推送通知服务。

    Here 是很好的推送通知教程

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      您的问题没有足够的细节。但我想你想问: 当您的应用程序安装并在设备上运行时,您的服务器会发送一条消息。 当您的设备收到消息时,它会发送响应。

      是的,这是可能的。当您的应用程序安装和运行时,您应该从 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));
              }
      

      --------------编辑---------------

      你应该通过C2DM。这是Android设备的推送通知服务,您必须遵循C2DM的整个过程。

      【讨论】:

      • 抱歉问题不清楚。我的意思是消息是从服务器端发送的,并且是由服务器发起的,就像 android 设备通过 servlet 接收来自其他用户的消息
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多