第三方API的简单调用

调用高德地图(amap)的web服务API

  1. 进入网站:http://lbs.amap.com/api/webservice/summary/
    申请key

  2. 需求:根据ip地址定位和拿到定位地方的天气

这里写图片描述

  1. 应用springmvc技术,具体实现:
    (1) 导包:
    这里写图片描述

    (2) 在类似于spring.xml作用的类中配置:

@Bean
    public RestTemplate restTemplate() {
        // 使用apache hc HttpClient库来负责底层请求发送
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        RestTemplate rt = new RestTemplate(requestFactory);
        //类型转换器  json 与 任何类型之间的转换
        List<HttpMessageConverter<?>> messageConverters = Arrays.asList(
                new MappingJackson2HttpMessageConverter()
        );

        rt.setMessageConverters(messageConverters);
        return rt;
    }

(3) 具体地图的service层:由于高德返回的是json格式的数据,我们接收数据是定义一个类来接收,这个类的属性与返回数据的json字段要相同。上面代码中用到了一个数据类型的转换器。

@Service
public class AmapServiceImpl implements AmapService {
    @Autowired
    private RestTemplate restTemplate;
    /*@Autowired
    private Environment env;*/
    @Override
    public AmapLocation getLocation(String ip) {
        //String key = env.getProperty("ipkey");  将key存于key.properties文件中
        String key = "c2cf7f844d01074663b4ccf33dfb2fb2";
        AmapLocation amapLocation = restTemplate.getForObject("http://restapi.amap.com/v3/ip?key={key}"
                + "&ip={ip}", AmapLocation.class,key,ip);
        return amapLocation;
        //第一个参数为 请求的地址,二个为接收的数据类型,三,四是参数的替代
    }
}

(4) 具体的天气service层:
实际代码跟上面的差不多,不同的是IP定位用的是类接收数据,因为网页上请求返回json数据只有一个层次,而天气有两个层次。
这里写图片描述

封装数据的实体类为:

public class Weather {
    private List<Content> lives;
    //Content类中为具体天气的属性
    public List<Content> getLives() {
        return lives;
    }
    public void setLives(List<Content> lives) {
        this.lives = lives;
    }
}

(5) 控制类:

@Controller
public class AmapController {
    @Autowired
    private AmapService amapService;
    @Autowired
    private WeatherService weatherService;

    @RequestMapping(method = RequestMethod.GET,value="/location")
    public String getAmapLocation(Model model,@RequestParam(required = false)String ip){
        //@RequestParam(required = false)获取页面传过来的参数,可有可无
        if(null!=ip){
            //获取地址
            AmapLocation amapLocation = amapService.getLocation(ip);
            model.addAttribute("location", amapLocation);
            model.addAttribute("ip", ip);

            //用ip定位返回的地址的adcode码来获取天气
            System.out.println(amapLocation);
            String adcode = amapLocation.getAdcode();
            System.out.println(adcode);
            Weather w = weatherService.getWeather(adcode);
            System.out.println("weather"+w);
            model.addAttribute("weather", w);
            return "amaplocation";
        }
        return "amaplocation";
    }
}

(6) 过程比较简单,得益于spring的支持。

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值