野狗实时通讯Utils

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woshimeihuo/article/details/88049848

野狗官网:https://www.wilddog.com/

野狗实时通讯使用步骤
1.注册野狗
2.创建应用
在这里插入图片描述
3.使用工具类添加数据,数据结构
在这里插入图片描述
4.野狗工具类
4.1 "test"是上传路径设置,还有相同的.child(“paht”)也可以设置路径

SyncReference myRef = WilddogSync.getInstance().getReference("test");
/**
 * 野狗推送工具类
 */
public class WilddogUtils {
    private static final Logger logger = LoggerFactory.getLogger(WilddogUtils.class);

 	/**
 	* 数据添加-方式1
 	*/
    public static Boolean add(JSONObject params) {
        //json格式组装(这一步可以选择传入参数的方法)
        JSONObject params = new JSONObject();
        params.put("code",200);
        params.put("message","调用成功");
        JSONObject data = new JSONObject();
        data.put("longitude","118.163926");
        data.put("latitude","24.477643");
        params.put("data",data);
        //初始化野狗组件
        WilddogOptions options = new WilddogOptions.Builder().setSyncUrl("https://野狗SyncAppID.wilddogio.com").build();
        WilddogApp.initializeApp(options);
        //同步参数设置
        SyncReference myRef = WilddogSync.getInstance().getReference("test");
        myRef.setValue(params);
    }
 /**
     * 数据添加-方式2
     * @return
     */
    public static Boolean addOrderNotice(Orders orders) {
        WilddogOptions options = new WilddogOptions.Builder().setSyncUrl("https://野狗SyncAppID.wilddogio.com").build();
        WilddogApp.initializeApp(options);
        SyncReference myRef = WilddogSync.getInstance().getReference("test");
        //由于对象中有BigDecimal类型的字段,而野狗不接收,所以,使用JSONUtils转换
        String ordersJson = JSONObject.toJSONString(orders);
        Object Str = JSONUtils.parse(ordersJson);
        myRef.setValue(Str, new SyncReference.CompletionListener() {
            @Override
            public void onComplete(SyncError error, SyncReference ref) {
                if (error != null) {
                    logger.error("error{}", error.toString());
                } else {
                    logger.info("success{}", "setValue success");
                }
            }
        });
        return true;
    }   

    /**
     * 根据url,获取野狗上的存储资源
     * @param dispatch_id
     * @return
     */
    public static JSONObject getDispatchDistance() {
        try {
            HttpResponse response = HttpUtils.doGet("https://wd3644337152ffntoj.wilddogio.com/test.json");
            BufferedReader b = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"),8*1024);
            String line=b.readLine();

            JSONObject resultObj = JSONObject.parseObject(line);
            if (null == resultObj){
                throw new BussinessException("调用接口失败");
            }
            Integer code = resultObj.getInteger("code");
            if (200 != code) {
                throw new BussinessException(resultObj.getString("message"));
            }
            return resultObj.getJSONObject("data");
        } catch (Exception e) {
            throw new BussinessException("调用接口失败");
        }
    }

}

注意事项

由于野狗的setValue()方法不接收obj中类型为BigDecimal的属性,可以使用方案2来解决

猜你喜欢

转载自blog.csdn.net/woshimeihuo/article/details/88049848