I'm writing an android app which must receive notifications from a java RESTful webservice ,like in gcm (google cloud messaging). If anyone knows how this is done, please point me to a tutorial. Thank you!

我正在编写一个Android应用程序,它必须接收来自java RESTful webservice的通知,例如gcm(谷歌云消息传递)。如果有人知道这是怎么做的,请指教我。谢谢!

1 个解决方案

#1


An android app can't receive HTTP messages like a server can. You can either use GCM or implement some kind of polling system that retrieves the information for you.

Android应用程序无法像服务器那样接收HTTP消息。您可以使用GCM或实现某种为您检索信息的轮询系统。

Weak solution: Using an alarm manager you could poll the server every 10 seconds for example and get any information that was stored for you.

弱解决方案:使用警报管理器,您可以每隔10秒轮询服务器,并获取为您存储的任何信息。

Better solution: Use long polling, by this I mean with your app you should send across a HTTP message and have it sit and wait for like 30 seconds, if no new data is sent to the server then it will just return with no data and go straight back again. If at any point during that 30 second period the server receives some data, then add it to a list and tell the message to release back to the phone with the list. That will only really work if you are expecting the same type of information everytime.

更好的解决方案:使用长轮询,我的意思是你的应用程序你应该通过HTTP消息发送并让它等待30秒,如果没有新数据发送到服务器然后它将返回没有数据和再次直接回去。如果在该30秒期间的任何时刻服务器接收到一些数据,则将其添加到列表中并告知该消息以释放回具有该列表的电话。只有当您每次都期待相同类型的信息时,这才会真正起作用。

Best solution: Again, use long polling. However, instead of releasing the HTTP message back to the phone with the new data, return a list of the new types of data received, like you could tell it there is a new text message waiting. The phone will then be aware of a pending message when it gets the response back, you can then just send a Get message and retrieve the message from a stored list.

最佳解决方案:再次使用长轮询。但是,不是使用新数据将HTTP消息发回电话,而是返回收到的新数据类型的列表,就像您可以告诉它有一条新的短信等待。然后,当手机获得响应时,手机将会知道待处理的消息,然后您可以发送Get消息并从存储的列表中检索消息。

Hope that helps, explained it kind of quick!

希望有所帮助,解释它有点快!

For help on building a Java RESTful service, look at this link: https://spring.io/guides/gs/rest-service/

有关构建Java RESTful服务的帮助,请查看以下链接:https://spring.io/guides/gs/rest-service/

I'm going off the assumption you already understand how the RESTful service works, and if not then that link should help. As far as an example for the 'Better solution', just start by making a basic service with a GET call that returns a string so you know it's working. When you have that going you just need a method to poll every x seconds that returns a list of the new data..

我假设您已经了解RESTful服务是如何工作的,如果没有,那么该链接应该有所帮助。至于“更好的解决方案”的一个例子,首先使用GET调用创建一个基本服务,该服务返回一个字符串,以便您知道它正在工作。当你有了这个时,你只需要一个方法来轮询每x秒返回一个新数据列表。

/**
 * This is the method you poll every x seconds
 * @return
 */
public List<Location> eventPoll() {
    ArrayList<Location> oldData = new ArrayList<>(m_Location);

    // small possibility this could get a new piece of data thats not in 'oldData' and
    // which would be cleared. Would be better looping through removing each piece
    // individually comparing it to the oldData
    m_Location.clear();

    return oldData;
}

I'm not sure how you plan on getting the location data into your service, but for the sake of simplicity just add it to an ArrayList like so..

我不确定您是如何计划将位置数据导入服务的,但为了简单起见,只需将其添加到ArrayList中即可。

/**
 * This is the method that stores the new data
 * @return
 */
public Location storeNewLocation(Location location) {
    m_Location.add(location);
}

** WARNING, not been tested. Literally just wrote it in here now **

**警告,未经测试。字面意思只是现在写在这里**

Again, this is all going off the assumption you know how to do the previous parts (building up the service), if not then that code isn't much help yet. Good luck!

同样,这一切都是假设您知道如何执行前面的部分(构建服务),如果没有,那么该代码还没有太多帮助。祝好运!

更多相关文章

  1. 关于android中sharedpreferences数据不更新的问题
  2. Android(监听+回调=观察者)模式从Dialog到Activity传递数据
  3. android获取网络数据
  4. android:使用网络通信技术从客户端直接获取服务端的对象数据
  5. 如何设计数据库模型来记录客户的历史活动?
  6. 为什么使用KML数据检索Android版Google方向论不再适用? [重复]
  7. 如何使用adb命令查看android中的数据库
  8. Android网络编程-----从服务器端获取xml数据并解析
  9. Android使用SQLite数据库(3)

随机推荐

  1. Android使用Google提供的地图实现定位时L
  2. Android实现自定义对话框(Dialog)
  3. Android 系统音量最大值的定义位置以及默
  4. android .apk 反编译
  5. Android(安卓)串口通信编程及串口协议分
  6. 【Android】Android Studio中gradle scri
  7. Flutter1.12与原生Android交互(kotlin)
  8. android开发 常用技巧
  9. android 源码导入到android studio
  10. 【译】Android 6.0接口变化(三)(Android 6.0