transistorsoft / capacitor-background-geolocation

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
MIT License
95 stars 16 forks source link

Cannot build App after applying the Android setup. Http POST requests won't work #24

Closed andreasaphilo closed 3 years ago

andreasaphilo commented 3 years ago

Your Environment

Latest Dependencies:

@capacitor/cli: 3.2.3 @capacitor/core: 3.2.3 @capacitor/android: 3.2.3 @capacitor/ios: 3.2.3

Installed Dependencies:

@capacitor/cli: 3.1.1 @capacitor/core: 3.1.2 @capacitor/android: 3.1.1 @capacitor/ios: 3.1.1

[success] iOS looking great! πŸ‘Œ`)

//the post request im trying to execute this.httpNative.sendRequest( "https://enterurlhere" { method: 'post', data: { //some data }, headers: { "Content-Type": "application/json", "Accept": "application/json", "Access-Control-Allow-Origin": "*", }, responseType: "json", }).then(data => { data.status = 0;//success console.log("success "); return data; }).catch(error => { if (error.status == 401) { error.status = -1; console.log("validate session") } else { error.status = 1;//error console.log("error"); } return error; });

## Expected Behavior
<!--- Tell us what should happen -->
The app should be able to communicate with the API and handle the responses in order for the user to login, logout, change password,... in our app
## Actual Behavior
<!--- Tell us what happens instead -->
The app is unable to execute any POST requests and stucks after applying the Android setup. After the build the app is able to open and execute GET requests. Whenever i try to execute a POST request the app returns the following error in my Console:

ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token U in JSON at position 0

## Steps to Reproduce
<!--- reproduce this issue; include code to reproduce, if relevant -->
1. Install capacitor-background-geolocation plugin
2. Install HTTP native plugin https://ionicframework.com/docs/native/http
3. Implement a simple http post request as shown above.
4. Follow the Android setup
5. Build the app
6. Try to run a post request
7. Check the console tab  for errors

## Context
<!--- What were you trying to do? -->

## Debug logs
<!-- include iOS / Android logs
- ios XCode logs,
- use #getLog / #emailLog methods (@see docs)
- Android: $ adb logcat
-->
<details>
    <summary>Logs</summary>

```<!-- Syntax highlighting:  DO NOT REMOVE -->
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token U in JSON at position 0
SyntaxError: Unexpected token U in JSON at position 0
    at JSON.parse (<anonymous>)
    at GeneralmethodsService.<anonymous> (/main-es2015.js:101)
    at Generator.next (<anonymous>)
    at /vendor-es2015.js:66673
    at new ZoneAwarePromise (/polyfills-es2015.js:1075)
    at __awaiter (/vendor-es2015.js:66669)
    at /main-es2015.js:89
    at ZoneDelegate.invoke (/polyfills-es2015.js:479)
    at Object.onInvoke (/vendor-es2015.js:57245)
    at ZoneDelegate.invoke (/polyfills-es2015.js:478)
    at resolvePromise (/polyfills-es2015.js:913)
    at /polyfills-es2015.js:820
    at rejected (/vendor-es2015.js:66671)
    at ZoneDelegate.invoke (/polyfills-es2015.js:479)
    at Object.onInvoke (/vendor-es2015.js:57245)
    at ZoneDelegate.invoke (/polyfills-es2015.js:478)
    at Zone.run (/polyfills-es2015.js:238)
    at /polyfills-es2015.js:972
    at ZoneDelegate.invokeTask (/polyfills-es2015.js:514)
    at Object.onInvokeTask (/vendor-es2015.js:57233)
defaultErrorLogger @ /vendor-es2015.js:34142 

christocracy commented 3 years ago

SyntaxError: Unexpected token U in JSON at position 0

Your error is not related to background-geolocation. You seem to have a JSON error with your data.

I would check the CHANGELOG of that plugin and seek support over there.

Also, why are you even using a 3rd party plugin for HTTP requests? background-geolocation has a built-in HTTP service. See API docs for HTTP Guide.

andreasaphilo commented 3 years ago

Hi Chris,

Thank you for quick response.

The reason i opened this issue in the first place was that the app with same plugins works just fine on iOS without any json issues.

The problem appears only on android. I will check the HTTP doc you pinted out, but what if i want to use the native http instead of your plugin http?

Do i have an option to choose the plugins i use'

I am i dependend to use the plugins that the plugin uses? πŸ™‚

Im looking forward to you are response on that.

christocracy commented 3 years ago

I wouldn't trust that "native http" plugin.

christocracy commented 3 years ago

There is a capacitor http plugin (though still doesn't use okhttp).

andreasaphilo commented 3 years ago

Can you show me an example or a link to an example on how to use the okhttp to make my POST,PUT,GET... API calls?

christocracy commented 3 years ago

Can you show me an example or a link to an example on how to use the okhttp to make my POST,PUT,GET... API calls?

The isn't something you do. okhttp is a native Android library. To use okhttp is to write Java code. That isn't something one does in a Capacitor app.

The plugin contains its own built-in http service. You just need to provide the plugin an url. See API docs "HTTP Guide".

andreasaphilo commented 3 years ago

Can you please send me a link to the HTTP GUIDE? It would be very helpful, thanks!

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Chris Scott @.> Sent: Monday, September 27, 2021 11:08:11 PM To: transistorsoft/capacitor-background-geolocation @.> Cc: Andreas Apostolou @.>; Author @.> Subject: Re: [transistorsoft/capacitor-background-geolocation] Cannot build App after applying the Android setup. Http POST requests won't work (#24)

Can you show me an example or a link to an example on how to use the okhttp to make my POST,PUT,GET... API calls?

The isn't something you do. okhttp is a native Android library. To use okhttp is to write Java code. That isn't something one does in a Capacitor app.

The plugin contains its own built-in http service. You just need to provide the plugin an url. See API docs "HTTP Guide".

β€” You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/transistorsoft/capacitor-background-geolocation/issues/24#issuecomment-928229513, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ATZWYFQ6HVYLVLMCGPM4J43UEDFKXANCNFSM5EVUVWVQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

christocracy commented 3 years ago

The README contains a link the the "API docs" in the table of contents.

andreasaphilo commented 3 years ago

This one? https://github.com/transistorsoft/capacitor-background-geolocation/blob/master/README.md

What i want is to make all the api calls (login, logout, ....) in my app through this...

API call request.post, request.get, request.put,.... API response handle.....

Not only Geolocation related....

Is this possible?

andreasaphilo commented 3 years ago

???

christocracy commented 3 years ago

Is this possible?

No. The plugin doesn't magically handle all your http requests.

andreasaphilo commented 3 years ago

So in order to make it clear.

I was using the ionic-native plugin to make my http requests GET, POST, PUT. Login, logout user, get user profile data from server...

And on top of that i wanted to use the geolocation plugin, but it wasnt possible and i needed to use the popular okkttp.

Apparently from what i understand i cannot have both. I mean i need to have geolocation plugin and my app functional. Can i?

Correct me if i am wrong.

christocracy commented 3 years ago

I mean i need to have geolocation plugin and my app functional. Can i?

Of course you can.

andreasaphilo commented 3 years ago

    return await BackgroundGeolocation.ready({
      url: "https://someurl",
      params: {
    //some params
      },
      headers: {
      //some headers
      },
    }, state => {
      console.log("[ready] success: ", state);
    }).catch(error => {
      return error;
    });```

    is this the correct way?
christocracy commented 3 years ago

Once the plugin is provided an url, it immediately attempts to POST every recorded location to the provided url. Adjust params, headers as your server requires.

andreasaphilo commented 3 years ago

i didn't manage to make it work. Can you provide me an example with pseudo code just to see how to make a simple post request(without the backgroundgeolocation) and handle the results? Just the skeleton.

i have followed the HTTP guide, https://transistorsoft.github.io/capacitor-background-geolocation/interfaces/httpevent.html. but the examples are related to BackgroundGeolocation

christocracy commented 3 years ago

i didn't manage to make it work

What makes you think it didn't work?

christocracy commented 3 years ago

You need to watch the plugin's logs in $ adb logcat.

$ adb logcat *:S TSLocationManager:V
andreasaphilo commented 3 years ago

Im watching my server logs but no requests are made, unless this works in a different way. All i need is a simple

request response

pseudo code!

kamekaioken commented 3 years ago

@andreasaphilo I think this is not the right place for your native http plugin related questions. Try finding an answer on the plugins github issue or through stackoverflow. I don't want to be mean, but I just think that your bugs are not related to this plugin and you will not find any help here which will show you any solution.

andreasaphilo commented 3 years ago

@andreasaphilo I think this is not the right place for your native http plugin related questions. Try finding an answer on the plugins github issue or through stackoverflow. I don't want to be mean, but I just think that your bugs are not related to this plugin and you will not find any help here which will show you any solution.

Im not trying to solve a bug at this stage! I just need an example of a server POST request. Can you provide me with one?

christocracy commented 3 years ago

There is no pseudo code for you.

Show me the result of $ adb logcat *:S TSLocationManager:V

andreasaphilo commented 3 years ago

I didn't need to use the adb logcat because im using IONIC framework and console.cloud.google to view server my logs. If you can give me a hint on how to proceed it would be helpful :)

christocracy commented 3 years ago

I didn't need to use the adb logcat

Yes you do. This is a very complex plugin and it is logging an immense amount of debugging information to logcat.

If you're not observing the logs, you will fail to use this plugin.

andreasaphilo commented 3 years ago

2021-09-28 18:51:48.328 32444-11918/my.app.name V/Capacitor: callback: 1177976, pluginId: BackgroundGeolocation, methodName: ready, methodData: {"options":{"url":"someurl","autoSync":true,"autoSyncThreshold":5,"params":{"username":"35799107978","password":"111111111"},"headers":{"Content-Type":"application\/json","Accept":"application\/json","Access-Control-Allow-Origin":"*","PHILO-OrgId":"499494","PHILO-ClientId":"jumpApp"}}}
2021-09-28 18:51:48.367 32444-11626/my.app.name D/TSLocationManager: [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [autoSyncThreshold, extras, headers, headlessJobService, params, schedule, url]
2021-09-28 18:51:48.372 32444-11626/my.app.name D/TSLocationManager: [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [autoSyncThreshold, headers, headlessJobService, params, url]
2021-09-28 18:51:48.381 32444-32444/my.app.name I/TSLocationManager: [c.t.l.http.HttpService flush] 
    ╔═════════════════════════════════════════════
    β•‘ HTTP Service (count: 0)
    ╠═════════════════════════════════════════════
2021-09-28 18:51:48.386 32444-32444/my.app.name I/TSLocationManager: [c.t.l.http.HttpService flush] 
    ╔═════════════════════════════════════════════
    β•‘ HTTP Service (count: 0)
    ╠═════════════════════════════════════════════
2021-09-28 18:51:48.393 32444-32444/my.app.name I/Capacitor/Console: File: http://localhost/main-es2015.js - Line 3623 - Msg: WOWOWWOWOWO[object Object]
2021-09-28 18:51:48.399 32444-32444/my.app.name I/Dialog: mIsSamsungBasicInteraction = false, isMetaDataInActivity = false
2021-09-28 18:51:48.403 32444-32444/my.app.name I/DecorView: [INFO] isPopOver=false, config=false
2021-09-28 18:51:48.403 32444-32444/my.app.name I/DecorView: updateCaptionType >> DecorView@be2a10e[], isFloating=true, isApplication=true, hasWindowDecorCaption=false, hasWindowControllerCallback=false```
christocracy commented 3 years ago

There's not going to be an http request until a location is recorded.

andreasaphilo commented 3 years ago

My app, is a loyalty app and has multiple screens makes multiple api calls. I am using this plugin for only one screen. The rest of the app is not related to the Background Geolocation. The users has the option to not enable their location services or not to go to that screen which is using the Background geolocation plugin.

I need to make calls to my server even if the user has not granted permission to GPS or even if the user never enter that screen.

Thats why i was asking you all this time.

christocracy commented 3 years ago

I need to make calls to my server even if the user has not granted permission to GPS or even if the user never enter that screen.

This is not related to the plugin. The plugin Configured with an url is concerned only with posting its own recorded locations. It has nothing to do with your general application HTTP requests.

andreasaphilo commented 3 years ago

I agree on what you are saying, the only issue is that after installing the plugin the general HTTP requests in my app are not working.

This is only happening to the Android version of the app and from our discussion the issue is related to the native okhttp which clashes with my ionic-native cordova plugin and causing my api calls to fail.

I need a solution for this.

The problem i am facing is a general issue which more people will came across and needs to be fixed!

christocracy commented 3 years ago

I don't have any idea how to help you. Your error is not from this plugin.

Zen0x7 commented 3 years ago

@andreasaphilo You should first ask for a location. HTTP Service (count: 0) that marks as you don't have any position on plugin database. When you have one pending location, the plugin will send to the backend. And also, how you can push locations to someurl?.

kamekaioken commented 3 years ago

@andreasaphilo If you have any conflicts with your http plugin, why don't you just switch to any javascript http/ajax library? For example if you're using Angular, just use the HttpClient from Angular. In general you won't need a native http plugin.

mobilemarines commented 3 years ago

If you are in need to do http requests in the background you will need a native http plugin. Currently only the Capacitor http plugin is able to do that after 5 minutes in background since Android 10. But the UI thread is paused, so no JS code is executed after 5 minutes in background. You will need to do background tasks with background fetch then to execute pre-defined time limited tasks.

andreasaphilo commented 3 years ago

@andreasaphilo If you have any conflicts with your http plugin, why don't you just switch to any javascript http/ajax library? For example if you're using Angular, just use the HttpClient from Angular. In general you won't need a native http plugin.

I am using Ionic Angular to build my mobile apps thats why i was using the ionic native plugin in the first place!