公司要调用一个天气的接口,最终选用阿里封装下的墨迹的API
就这个:墨迹天气(专业版CityID)全国天气_API专区_云市场-阿里云
然后在这个事例的基础上改了一个我的接口
但是一直访问不通!!!
一直没怀疑是阿里提供的东西会有问题,响应式一直是timeOut
我一直找我自己的问题,想着是不是token没配好,或者appKey什么的问题……
万万没想到是示例代码里面提供的httpUtils工具类有问题
换一个工具类就好了
下面是改过之后的代码,有需要的自取
private WheatherVO getWheatherVO(Integer cityId) {
WheatherVO wheatherVO = null;
String url = screenProperties.getUrl();
String appcode = screenProperties.getAppcode();
Map<String, Object> bodys = new HashMap<String, Object>();
bodys.put("cityId", cityId);
bodys.put("token", screenProperties.getToken());
String authorization = HttpRequest.post(url)
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.header("Authorization", "APPCODE "+appcode)
.form(bodys).execute().body();
JSONObject jsonObject = JSON.parseObject(authorization);
JSONObject condition = jsonObject.getJSONObject("data").getJSONObject("condition");
JSONObject city = jsonObject.getJSONObject("data").getJSONObject("city");
Integer temp = condition.getInteger("temp");
Integer humidity = condition.getInteger("humidity");
String windDir = condition.getString("windDir");
Integer windLevel = condition.getInteger("windLevel");
String tips = condition.getString("tips");
wheatherVO = WheatherVO.builder()
.condition(condition.getString("condition"))
.temp(temp + "°C")
.humidity(humidity + "%")
.windDir(windDir)
.windLevel(windLevel + "级")
.tips(tips)
.cityName(city.getString("name"))
.cityId(city.getInteger("id"))
.build();
return wheatherVO;
}
WheatherVO:
package com.mmscm.screen.vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Data;
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@Builder
public class WheatherVO {
/**
* 城市名称
*/
private String cityName;
/**
* 城市ID
*/
private Integer cityId;
/**
* 温馨提示(冷热适宜,感觉很舒适)。
*/
private String tips;
/**
* 温度 -12度
*/
private String temp;
/**
* 湿度 (45%)
*/
private String humidity;
/**
* 风力 (3级)
*/
private String windLevel;
/**
* 风向 (东北风)
*/
private String windDir;
/**
* 天气状况
*/
private String condition;
}