Java calls the rest API of Supermap iserverjava6r

Source: Internet
Author: User

Recently, someone asked Java how to access the Supermap iserverjava6r service. Because Supermap iserverjava6r currently does not support RMI access, it is recommended that you use Java to directly access the rest API.

Environment preparation:

1. Development Tool myeclipse, server iserverjava6r

2. httpcomponents and org. JSON class libraries. The first open-source Java client tool library for the HTTP protocol. org. JSON is the JSON library. Both are in the iserverjava6r dependency library and can be directly called. For the class library used, you can directly in the iserverjava6r installation directory \ webapps \ iServer \ WEB-INF \ Lib find:

3. iserverjava6r rest API documentation

 

Development ideas:

1. Construct a parameter class based on the Rest API.

2. Call httpcomponents to send the request and parse the result

3. Display Results. Generally, map is used.

 

To implement a simple task, call distance to calculate the distance.CodeRight:

1. parameter class. The distance interface requires two parameters: A point2d array and an enumeration unit. I usually use meter as the unit, so I use meter directly.

Parameter class/*** Parameter class ** @ author awei20081 **/public class point2d {private Double X; private Double Y; Public point2d (Double X, Double Y) {This. X = x; this. y = y;} public double getx () {return X;} public void setx (Double X) {This. X = x;} public double Gety () {return y;} public void sety (Double Y) {This. y = y ;}}

2. Send a request and parse it. Here, the distance interface is a GET request, so we construct an httpget object that calls httpcomponents. The Code is as follows:

 Request issuance and resolution  Import Java.net. url; Import Java.net. urlencoder; Import Java. util. arraylist; Import Org. Apache. http. httpresponse; Import Org. Apache. http. Client. Methods. httpget; Import Org. Apache. http. impl. Client. defaulthttpclient; Import Org. Apache. http. util. entityutils; Import Org. JSON. jsonobject; Public   Class Getdemo { Public   Final   Static   Void Main (string [] ARGs) Throws Exception {// Construct the request URL String url =" Http: // localhost: 8090/iServer/services/Map-World/rest/maps/% E4 % B8 % 96% E7 % 95% 8C % E5 % 9C % B0 % E5 % 9B % Be/distance. rjson? "; // Construct parameters Arraylist <point2d> arraylist = New Arraylist <point2d> (); arraylist. Add ( New Point2d (0, 0); arraylist. Add ( New Point2d (10, 10 )); // Get URL. URL uri = New URL (URL + urlencoder. encode (" Point2ds = "+ Tojsonarray (arraylist ),"UTF-8 ") +" & Amp; unit = Meter "); Httpget = New Httpget (URI. tostring ()); // Execute the http get request Httpresponse response = New Defaulthttpclient(cmd.exe cute (httpget ); // Send get and return an httpresponse object. Compared with post, the namevaluepair array is not added as a parameter.  If (Response. getstatusline (). getstatuscode () = 200 ){ // If the status code is 200, a normal response is returned. String result = entityutils. tostring (response. getentity ()); // Obtain the returned string Jsonobject jsresult = New Jsonobject (result); system. Out. println (" The length is "+ Jsresult. Get (" Distance ") +"  "+ Jsresult. getstring (" Unit ")); // Print the output }} Public   Static Stringbuilder tojsonarray (arraylist <point2d> arraylist) {stringbuilder string = New Stringbuilder (); string. append (" [ "); For ( Int I = 0; I <arraylist. Size ()-1; I ++) {jsonobject jsobject = New Jsonobject (arraylist. Get (I); string. append (jsobject. tostring () +" , ");} Jsonobject jsobject = New Jsonobject (arraylist. Get (arraylist. Size ()-1); string. append (jsobject. tostring (); string. append (" ] "); Return String ;}}

Note: This is a bit of a struggle. I encountered many problems with httpcomponents first. For more information, see:

(1) The URL can be obtained directly, that is, http: // localhost: 8090/iServer/services/Map-World/rest/maps/World Map/distance. rjson, but it cannot be used directly, because it needs to encode Chinese characters and cannot directly call urlencoder to encode the whole URL. Here, we simply encode the world map as follows: /% E4 % B8 % 96% E7 % 95% 8C % E5 % 9C % B0 % E5 % 9B % be, so the URL isHttp: // localhost: 8090/iServer/services/Map-World/rest/maps/% E4 % B8 % 96% E7 % 95% 8C % E5 % 9C % B0 % E5 % 9B % Be/distance. rjsonThe URL can be parsed one by one for encoding.

(2) If you directly use the jsonarray In the org. JSON class library to construct a DNS point2d array, the resolution result is not very correct. Therefore, I directly construct a tojsonarray to process the point2d array.
(3) The default browser uses UTF-8 encoding for URL encoding, and iserverjava6r encodes the returned results of the request into UTF-8.

3. The result is displayed. In this example, the println function is called to output the result.

The length is 1570277.6641936298 meters.

 

The above is just a simple introduction. It may be very simple for masters, but after all, record it for the first time and check it later.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.