如何在本机后台服务中使用(角度)HTTP客户端-NativeScript [英] How to use (angular) HTTP Client in native background service - NativeScript

查看:81
本文介绍了如何在本机后台服务中使用(角度)HTTP客户端-NativeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在后台服务(android)中使用有角度的http客户端.

How can i use an angular http client in my background service (android).

我的应用程序需要将数据从后台服务发送到我的服务器.

My app needs to send the data from the background service to my server.

我正在使用NativeScript/Angular.

Im using NativeScript / Angular.

我的后台服务

declare var android;

if (application.android) {
    (<any>android.app.Service).extend("org.tinus.Example.BackgroundService", {
        onStartCommand: function (intent, flags, startId) {
            this.super.onStartCommand(intent, flags, startId);
            return android.app.Service.START_STICKY;
        },
        onCreate: function () {
            let that = this;

            geolocation.enableLocationRequest().then(function () {
                that.id = geolocation.watchLocation(
                    function (loc) {

                        if (loc) {
                            // should send to server from here

                        }
                    },
                    function (e) {
                        console.log("Background watchLocation error: " + (e.message || e));
                    },
                    {
                        desiredAccuracy: Accuracy.high,
                        updateDistance: 5,
                        updateTime: 5000,
                        minimumUpdateTime: 100
                    });
            }, function (e) {
                console.log("Background enableLocationRequest error: " + (e.message || e));
            });
        },
        onBind: function (intent) {
            console.log("on Bind Services");
        },
        onUnbind: function (intent) {
            console.log('UnBind Service');
        },
        onDestroy: function () {
            geolocation.clearWatch(this.id);
        }
    });
}

我尝试过的两种方法.

(1).使用Injector注入我的服务

(1). Using Injector to inject my service

         const injector = Injector.create([ { provide: ExampleService, useClass: ExampleService, deps: [HttpClient] }]);
         const service = injector.get(ExampleService);
         console.log(service.saveDriverLocation); // This prints
         service.saveDriverLocation(new GeoLocation(loc.latitude, loc.longitude, loc.horizontalAccuracy, loc.altitude), ['id']); // This complains 

问题(1)

System.err: TypeError: Cannot read property 'post' of undefined

(2).使用本机代码

     let url = new java.net.URL("site/fsc");
     let connection = null;
     try {
          connection = url.openConnection();
     } catch (error) {
           console.log(error);
     }

     connection.setRequestMethod("POST");
     let out = new java.io.BufferedOutputStream(connection.getOutputStream());
     let writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(out, "UTF-8"));
     let data = 'mutation NewDriverLoc{saveDriverLocation(email:"' + (<SystemUser>JSON.parse(getString('User'))).email + '",appInstanceId:' + (<ApplicationInstance>JSON.parse(getString('appInstance'))).id + ',geoLocation:{latitude:' + loc.latitude + ',longitude:' + loc.longitude + ',accuracy:' + loc.horizontalAccuracy + '}){id}}';
     writer.write(data);
     writer.flush();
     writer.close();
     out.close();
     connection.connect();

问题(2)

System.err: Caused by: android.os.NetworkOnMainThreadException

所以基本上第一种方法是有角度的,问题是我没有注入所有需要的服务/不确定如何使用.

So basically the first approach is angular, issue is that im not injecting all the needed services / not sure how.

第二种方法是本机的,问题在于网络位于主线程上.我需要使用AsyncTask只是不确定

Second approach is native, and the issue is the network is on the main thread. I need to use AsyncTask just not sure how

推荐答案

请查看此链接 如何修复android.os.NetworkOnMainThreadException?

将以下内容添加到您的本机代码中,就像在选项2中提到的那样.它应该可以工作

Add the following to your native code like you mention in option 2. And it should work

let policy = new 
android.os.StrictMode.ThreadPolicy.Buiilder().permitAll().build();
andriod.os.StrictMode.setThreadPolicy(policy);

这篇关于如何在本机后台服务中使用(角度)HTTP客户端-NativeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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