Closed andreasaphilo closed 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.
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.
I wouldn't trust that "native http" plugin.
background-geolocation
uses okhttp
for its HTTP requests.There is a capacitor http plugin (though still doesn't use okhttp
).
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?
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".
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.
The README contains a link the the "API docs" in the table of contents.
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?
???
Is this possible?
No. The plugin doesn't magically handle all your http requests.
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.
I mean i need to have geolocation plugin and my app functional. Can i?
Of course you can.
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?
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.
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
i didn't manage to make it work
What makes you think it didn't work?
You need to watch the plugin's logs in $ adb logcat
.
$ adb logcat *:S TSLocationManager:V
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!
@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 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?
There is no pseudo code for you.
Show me the result of $ adb logcat *:S TSLocationManager:V
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 :)
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.
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```
There's not going to be an http request until a location is recorded.
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.
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.
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!
I don't have any idea how to help you. Your error is not from this plugin.
@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?.
@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.
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 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!
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! π`)
#ready
://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; });
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token U in JSON at position 0