mauron85 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
1.33k stars 559 forks source link

Sending data format #539

Open Witexp opened 3 years ago

Witexp commented 3 years ago

At the moment, the application sends data in the form of an array, but not all servers can accept this data format. Is it possible to wrap this array in an object? Original - [{ "lat": '@latitude', "lon": '@longitude', "dt": '@time' }] Needed - {"data": [{ "lat": '@latitude', "lon": '@longitude', "dt": '@time' }] }

Witexp commented 3 years ago

Good day. Now data is sent as an array of objects, often servers do not want to work with this format and you need to wrap the array in an object.

I suggest adding a parameter for the component - httpTemplateRoot: ‘data’

For the format of the sent data to be an Object Example: { data: [ { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, ] } Now I solved this problem by changing the code for sending online in the file - PostLocatiionTask JSONArray jsonLocations = new JSONArray(); JSONObject jsonObjectLocation = new JSONObject(); // added

jsonLocations.put(mConfig.getTemplate().locationToJson(location));
jsonObjectLocation.put("data",jsonLocations);   // added

responseCode = HttpPostService.postJSON(url, jsonObjectLocation, mConfig.getHttpHeaders()); // changed from jsonLocations to jsonObjectLocation

I also made corrections in the formation of data for sending with forceSync in the file - BatchManager

writer = new LocationWriter(fs, template); writer.beginObject(); // added writer.name("data"); // addedо writer.beginArray(); while (cursor.moveToNext()) { BackgroundLocation location = BackgroundLocation.fromCursor(cursor); writer.write(location); } writer.endArray(); writer.endObject(); // added writer.close(); fs.close();

For this, I also added the beginObject and endObject methods in the LocationWriter class.

But all my changes will be gone when I update or if I want to clean up node_modules I really look forward to your help in adding this parameter - httpTemplateRoot

Witexp commented 3 years ago

Good day I managed to add a parameter for the component myself - httpTemplateRoot And the data to send can be of the form - { httpTemplateRoot: [ { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, ] }

If you do not specify it, then the data is sent in the old way as an array - [ { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, { lat: '@latitude',lon: '@longitude', dt: '@time' }, ]

I have done some refinement for both a single online submission and a bulk submission using forceSync ()

May I suggest these add-ons for a component of your component? How can I best provide them to you? Pull requests?