如何排队在Android上的服务器发送数据 [英] How to queue up data for server dispatch on android

查看:345
本文介绍了如何排队在Android上的服务器发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与电子邮件功能的Andr​​oid应用程序。我希望我的用户能够撰写并在飞行模式下发送邮件时。为此,我需要某种形式的队列,可以检查是否有网络和发送,等我像这一定是做了次100S。但我真的不知道为什么我的搜索不转动起来了。有谁知道一个库或Git项目,我可以用它来做到这一点的?如果没有,没有人知道如何做到这一点?

我相信这就是所谓的队列,并发送模式

更新

我开始在这个问题上的赏金。我希望的是,不使用SMS工作的例子。对于我的具体情况我正在联系的Andr​​oid项目的AppEngine上。客户端需要发送数据(下一个特定的POJO字符串,位图等说狗)到服务器。我希望能够排队这些数据以某种方式。我可以用GSON来保存数据,文件等的底线是,我需要能够检查网络。当有网络我出列,我排队到服务器中。如果没有网络,我一直保存到队列中。

我的队列可问答LT;狗&GT; ,其中是我的类,如<$ C场$ C>位图(或路径图像),字符串等。

我在找工作的例子。它可以是很简单的,但例如必须工作。一个Git压缩将是巨大的。我给我一半的点放弃收盘这个问题。

 类狗{
   字符串dogname;
   字符串映像路径;
   INT dogAge;
   //等等。
}

//发送犬服务器设计模式
0)和解组从文件中使用GSON队列
1)添加狗排队
2)如果有网络,遍历队列,并发送数据到服务器
3)如果不存在网络保存队列到文件

//在理想情况下,只要存在网络,该方法应能够检测这样和运行数据发送到服务器
 

解决方案

首先,您需要设置一个接收器观看WiFi连接时看到他们的数据,你也可以检查正常的3G / 4G连接,使广播接收器这一点。 TODO这让使用实施广播接收器连接状态的变化。把这样的事情在清单中的应用程序标签

 &LT;接收机器人:名称=。NetworkChangeReceiver&GT;
       &LT;意向滤光器&gt;
           &lt;作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/&GT;
       &所述; /意图滤光器&gt;
&LT; /接收器&GT;
 

现在我们需要,我们只是在清单中定义的接收器

 公共类NetworkChangeReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        //这里,请检查网络连接是否可用。如果是的话,开始你的电子邮件服务。如果没有,请停止你的电子邮件服务。
       ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
       的NetworkInfo信息= cm.getActiveNetworkInfo();
       如果(资讯!= NULL){
           如果(info.isConnected()){
               //启动服务
               意向意图=新的意图(这一点,ItemServiceManager.class);
               startService(意向);
           }
           其他 {
               //一站式服务
               意向意图=新的意图(这一点,ItemServiceManager.class);
               stopService(意向);
           }
       }
    }
}
 

这样做是把一个大胖子的天线称为 NetworkChangeReceiver 在Android的土地,也就是微调,以时Android有话要说的改变监听数据连接状态。

现在你需要建立你的 ItemServiceManager.class 这应该从数据库中读取(它也应该扩展服务它应选择最古老的项目数据库,(通过电子邮件发送,文字吧,上传到服务器,等等),如果连接成功,那么从数据库中删除的项目,并加载下一个最早的之一。如果没有更然后关闭的服务和广播接收器。

如果你有一个连接,用户需要发送更多的数据,然后将其添加到数据库中,并确保该服务已启动。也许告知了它应加倍决定它可以关闭,因为无所不有前检查数据库(几秒钟后)。

这是如何可能会禁用广播接收器。

  PackageManager packageManager = context.getPackageManager();
组件名组件名=新的组件名(背景下,NetworkChangeReceiver.class);
        packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
 

当一个新的项目将被上载的,如果没有网络连接,电子邮件应保存到数据库和广播接收器应开始知道何时互联网是背面,因此它可以知道何时上载。你可能会启动它是这样的。

  PackageManager packageManager = context.getPackageManager();
组件名组件名=新的组件名(背景下,NetworkChangeReceiver.class);
        packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
 

整点是,你只在乎连接广播,当你有一些储存被上传,但不能上传,因为没有数据连接起来。当你什么都没有上传,不要被周围保持您的接收器/服务废物处理和电池。而当你有电子邮件等,然后启动你的BroadcastReceiver,要知道,当你有数据连接,这样你就可以开始上传。

我不认为任何人会写一整个工作为您解决,希望这是绰绰有余,让你对你的方式。

编辑:

您可以做的另一件事,是让服务器允许接受您的项目的数组,你可以把它上传一次全部,当你得到一个有效的连接的方式。一般来说,你会做这个,如果每个项目安葬小。但是,如果你上传的图片或视频或任何大的,最好做一次大概。

I am working on an android app with an email feature. I want my users to be able to compose and send emails while in airplane mode. For that I need some sort of queue that can check if there is network and send, etc. I image this must have been done 100s of times. But I am not really sure why my searches aren't turning up much. Does anyone know of a library or git project that I can use to accomplish this? If not, does anyone know how to accomplish this?

I believe it is called the Queue and send pattern.

Update

I am starting a bounty on this question. What I hope for is a working example that does not use SMS. For my particular case I am working on an Appengine Connected Android Project. The client needs to send data (String, Bitmap, etc under a particular POJO say Dog) to the server. I want to be able to queue up these data somehow. I can use Gson to save data to file, etc. The bottom line is that I need to be able to check for network. When there is network I dequeue my queue into the server. If there is no network, I keep saving into the queue.

My queue can be Queue<Dog>, where Dog is my class with fields such as Bitmap (or path to image), String, long, etc.

I am looking for a working example. It can be very simple, but the example must work. A git zip would be great. I am giving up close to half of my points for this question.

class Dog{
   String dogname;
   String pathToImage;
   int dogAge;
   //etc.
}

//Design pattern for sending Dog to server
0) Unmarshall queue from file using Gson
1) Add dog to queue
2) If there is network, loop through queue and send data to server
3) if there is no network save queue to file

//Ideally, as soon as there is network, the method should be able to detect so and run to send data to server

解决方案

First you need to set up a receiver to watch the wifi connection to see when they have data, you could also check for normal 3g/4g connections and make a broadcast receiver for that as well. todo this let use implement a broadcast receiver for connection status changes. put something like this in the manifest in the application tag

<receiver android:name=".NetworkChangeReceiver" >
       <intent-filter>
           <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
       </intent-filter>
</receiver>

now we need to make the receiver we just defined in the manifest

public class NetworkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //here, check that the network connection is available. If yes, start your email service. If not, stop your email service.
       ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo info = cm.getActiveNetworkInfo();
       if (info != null) {
           if (info.isConnected()) {
               //start service
               Intent intent = new Intent(this, ItemServiceManager.class);
               startService(intent);
           }
           else {
               //stop service
               Intent intent = new Intent(this, ItemServiceManager.class);
               stopService(intent);
           }
       }
    }
}

What this does is puts a big fat antenna called NetworkChangeReceiver out in android land, that is fine tuned to listen in on when android has something to say about a change in the data connection status.

now you need to build your ItemServiceManager.class which should read from a database (it should also extend Service. It should choose the oldest item in the database, (email it, text it, upload to server, whatever), and if the connection was successful then remove the item from the database, and load the next oldest one. If there is no more then close the service and the broadcast receiver.

If you have a connection and the user needs to send more data, then add it to the database, and then make sure the service is started. Maybe notify it that it should double check the database (after a few seconds) before deciding it can close because nothing is there.

This is how you might disable your broadcast receiver.

PackageManager packageManager  = context.getPackageManager();
ComponentName componentName = new ComponentName(context, NetworkChangeReceiver.class);
        packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

When a new item is to be uploaded, if there is no web connection, the email should be saved to the database and the broadcast receiver should be started to know when internet is back so it can know when to upload. You might start it up like this.

PackageManager packageManager  = context.getPackageManager();
ComponentName componentName = new ComponentName(context, NetworkChangeReceiver.class);
        packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

The whole point is you only care about connection broadcasts when you have something stored to be uploaded but can not upload it because of no data connection. When you have nothing to upload, don't waste processing and battery by keeping your receiver/service around. And when you do have emails waiting, then start up you broadcastreceiver, to know when you have data connection so that you can start uploading.

I do not think anyone is going to write a whole working solution for you, hopefully this is more than enough to get you on your way.

Edit:

Another thing you can do, is let the server allow acceptance of an array of your items, that way you can just upload it all at once when you get a valid connection. Generally you would do this if each item was decently small. But if you are uploading pictures or videos or anything large, best to do it one at a time probably.

这篇关于如何排队在Android上的服务器发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆