1 Overview
Sometimes we need to store business data on mobile devices to ensure that the data is accessible offline. There are many ways to store offline data. This article describes how to store business data through JSON files, and then read and draw data on the android client.
2. Prepare a JSON File
First, you need to prepare a JSON file. In this example, I have published business data to the map service. Therefore, we can use the element query method of the ArcGIS rest service to obtain data in JSON format.
Is the service layer used for testing:
Find the "query" button at the bottom of the "Education and Scientific Research _ point_wm" layer page in the "Beijing" map service, for example:
Click the query button to go to the element query page, for example:
Here, you can set query filtering conditions as needed. This article sets the constant condition "1 = 1" to obtain all elements, set the output field to all fields, and return element ry, in addition, you must set the format of the returned result to JSON, for example:
After setting the query conditions and return parameters, click a query button to obtain the element set in JSON format that meets the conditions, for example:
Copy all the content, create a new text file, paste the content, and save the file in features. JSON format, such:
Now the business data of the JSON file is ready and uploaded to the Android mobile phone or tablet for reading.
3. Android client reading
Because ArcGIS Android SDK provides on-site calling interfaces, the client only needs several linesCodeYou can load the JSON file and draw it out in graphic. The key code is as follows:
Jsonfactory factory = new jsonfactory (); Uri uri = new uri ("file: // MNT/sdcard/basemap/Beijing/poi/features2.json "); file file = new file (URI); jsonparser = factory. createjsonparser (New fileinputstream (File); featureset features = featureset. fromjson (jsonparser); graphic [] graphics = features. getgraphics (); graphicslayer poi = new graphicslayer (); simplemarkersymbol sym = new simplemarkersymbol (color. blue, 8, simplemarkersymbol. style. circle); simplerenderer Renderer = new simplerenderer (sym); poi. setrenderer (Renderer); poi. addgraphics (graphics); map. addlayer (POI );
With the above code, you can load the element set in the JSON file to the graphicslayer, but the following error is reported during actual operation:
Analysis found that the original encoding format problems, need to save the JSON file as the UTF-8 format, so the features. JSON save another, such:
Save the file and upload it to your mobile phone or tablet. Then, test the result as follows:
The Blue Points on the graph are the business data in JSON format loaded and can be used for subsequent query and analysis operations.