Don't talk nonsense, go directly to the code, see the attachment for details, replace the AK in the code with your own AK
package com.bugyun.constant; /** * Parameter constant for calling Baidu request * @author Administrator * */ public class RequestArgsConstant { /** * Baidu map ak */ public static enum AK { /** * Baidu map server AK */ SERVER("ak","1VXoHnmOFbX9h5yNfpdL9hnf6GPaFM11"), /** * Baidu map browser AK */ JS("ak","CYSQLGty1YSUGDmI2vkmdtwTQ14L0311"); private String key; private String value; private AK( String key , String value){ this.key = key ; this.value = value ; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }
package com.bugyun.constant; /** * Call the URL constant requested by Baidu * POST: create, update, delete * GET: query * @author Administrator * */ public class RequestURLConstant { /** * Creation of location data table (geotable) */ public final static String GEOTABLE_CREATE= "http://api.map.baidu.com/geodata/v3/geotable/create"; /** * Query of location data table (geotable) */ public final static String GEOTABLE_QUERY= "http://api.map.baidu.com/geodata/v3/geotable/list"; /** * Update of location data table (geotable) */ public final static String GEOTABLE_UPDATE= "http://api.map.baidu.com/geodata/v3/geotable/update"; /** * Delete location data table (geotable) */ public final static String GEOTABLE_DELETE= "http://api.map.baidu.com/geodata/v3/geotable/delete"; }
package com.bugyun.main; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import org.apache.http.Consts; import org.apache.http.message.BasicNameValuePair; import com.bugyun.constant.RequestArgsConstant; import com.bugyun.constant.RequestURLConstant; import com.bugyun.util.HttpClientUtil; public class ApplicationEntry { public static void main(String[] args) { // Creation of location data table (geotable) Map<String, String> map = new HashMap<>(); map.put(RequestArgsConstant.AK.SERVER.getKey(), RequestArgsConstant.AK.SERVER.getValue()); map.put("name","geotableCreateOne"); map.put("geotype", "1"); map.put("is_published", "1"); String createStr = HttpClientUtil.doPost(RequestURLConstant.GEOTABLE_CREATE, Consts.UTF_8.toString(), map); System.out.println(createStr); // query of location data table (geotable) // BasicNameValuePair bnvpOne = new BasicNameValuePair(RequestArgsConstant.AK.SERVER.getKey(), RequestArgsConstant.AK.SERVER.getValue()); // BasicNameValuePair bnvpTwo = new BasicNameValuePair("name", "geotableCreateOne"); // String getStr = HttpClientUtil.doGet(RequestURLConstant.GEOTABLE_QUERY, "UTF-8", bnvpOne, bnvpTwo); // System.out.println(getStr); // update the location data table (geotable) // Map<String, String> updateMap = new HashMap<>(); // updateMap.put(RequestArgsConstant.AK.SERVER.getKey(), RequestArgsConstant.AK.SERVER.getValue()); // updateMap.put("name", "geotableCreateOneUpdate"); // //ID value: the newly created ID above // updateMap.put("id", "177625"); // String updateStr = HttpClientUtil.doPost(RequestURLConstant.GEOTABLE_UPDATE, Consts.UTF_8.toString(), updateMap); // System.out.println(updateStr); // delete location data table (geotable) // Map<String, String> deleteMap = new HashMap<>(); // deleteMap.put(RequestArgsConstant.AK.SERVER.getKey(), RequestArgsConstant.AK.SERVER.getValue()); // //ID value: the newly created ID above // deleteMap.put("id", "177629"); // String deleteStr = HttpClientUtil.doPost(RequestURLConstant.GEOTABLE_DELETE, Consts.UTF_8.toString(), deleteMap); // System.out.println(deleteStr); } }
package com.bugyun.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * Call the third interface request * @author Administrator * */ public class HttpClientUtil { /** * Get request call * @param url URL without parameters * @param charset character encoding (constant under the Consts class) * @param basicNameValuePair Create basicNameValuePair(key, value) object, parameter key: key after url, value: value after url equal sign * @return Call the URL to return the result. The value of the field status in the returned result means:<br>0: Success<br>1: Internal server error<br>2: Parameter error<br>3: http method error<br>21: This operation is Batch operation<br>22: Failed to synchronize to retrieval<br>31: Server lock failed<br>32: Server failed to release lock<br>1001: The name of the table is duplicate<br>1002: The number of tables has reached Maximum value<br>1003: There is poi data in the table, and deletion is not allowed<br>2001: The key of the column is duplicated<br>2002: The key of the column is a reserved field<br>2003: The number of columns has reached the maximum value<br> >2004: Only one unique index can be created<br>2005: Failed to update to unique index, there are duplicates in the original poi data<br>2011: Sort filter field can only be used for integer or decimal type columns<br>2012: Sort The filtered column has reached the maximum value<br>2021: The retrieved field can only be used for string type columns and the maximum length cannot exceed 512 bytes<br>2022: The retrieved column has reached the maximum value<br>2031 : The indexed column has reached the maximum value<br>2041: The specified column does not exist<br>2042: Modify max_length must be greater than the original value<br>3001: Update coordinates must contain latitude and longitude and type<br>3002: Unique index Duplicate fields<br>3031: Uploaded file is too large<br> * */ public static String doGet(String url, String charset,BasicNameValuePair... basicNameValuePair) { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String result = null; // encapsulate request parameters List<NameValuePair> params = new ArrayList<>(); for (int i = 0; i < basicNameValuePair.length; i++) { params.add(basicNameValuePair[i]); } String str = ""; try { // Convert to key-value pair str = EntityUtils.toString(new UrlEncodedFormEntity(params, charset)); // create a Get request HttpGet httpGet = new HttpGet(url + "?" + str); // Execute the Get request, response = httpClient.execute(httpGet); // See if the request is successful, here is the http status code printed // System.out.println(response.getStatusLine().getStatusCode()); // get the response body HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, charset); } } catch (ParseException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } finally { // consume entity content if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace (); } } // Close the corresponding discarded http connection if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace (); } } } return result ; } /** * Post request call * @param url URL without parameters * @param charset character encoding (constant under the Consts class) * @param map parameter key: key after url, value: value after url equal sign * @return */ @SuppressWarnings("unchecked") public static String doPost(String url, String charset , Map<String, String> map) { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null ; HttpPost httpPost = null; String result = null; try { httpPost = new HttpPost(url); // Setting parameters List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator<?> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> elem = (Entry<String, String>) iterator.next(); String key = elem.getKey() ; String value = elem.getValue() ; BasicNameValuePair bnvp = new BasicNameValuePair(key, value); list.add(bnvp); } if (list.size() > 0) { // Convert to key-value pair UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } // Execute Post request, response = httpClient.execute(httpPost); if (response != null) { // See if the request is successful, here is the http status code printed // System.out.println(response.getStatusLine().getStatusCode()); // get the response body HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception ex) { ex.printStackTrace(); }finally { // consume entity content if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace (); } } // Close the corresponding discarded http connection if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace (); } } } return result; } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bugyun</groupId> <artifactId>baiduMap</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies> </project>