xieb
2024-05-07 7e36f0a433e588bac2c62db0bddb0109b593dcc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.dji.sample.amap.service.impl;
 
import com.alibaba.druid.support.json.JSONUtils;
import com.dji.sample.amap.service.IAmapService;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
@Service
public class AmapServiceImpl implements IAmapService {
 
    private  final String AMAP_KEY = "6c3ea75b215f0c0efcbcfdf13273991b";
    private  final String searchByKeywordUrl = "https://restapi.amap.com/v3/assistant/inputtips?&datatype=poi&key=" + AMAP_KEY + "&keywords=";
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Override
    public Object searchByKeyword(String keyword) {
 
        Object result = apiRequestResult(searchByKeywordUrl+keyword);
 
 
        return result;
    }
 
    private Object apiRequestResult(String url) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //封装请求头
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
        try {
            //有请求头,有参数请求
            ResponseEntity<String> responseEntity =
                    restTemplate.exchange(url,
                            HttpMethod.GET,
                            formEntity,
                            String.class);
            String body = responseEntity.getBody();
 
            Object parse = JSONUtils.parse(body);
 
            // 返回
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}