您的位置:首页 > 理论基础 > 计算机网络

通过httpClient访问第三方API

2017-02-28 14:44 666 查看
一个访问知心天气的demo,知心天气官方API详情可见:http://www.thinkpage.cn/doc#now

这里只做其中的逐日天气预报和昨日天气 ,此接口属于其中的免费接口,可以获取指定地区,指定时间段内的天气信息。

创建项目,引入依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Springboot 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>

4000
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- httpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<!-- fastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>


创建请求实体ZhiXinRequest

private String method;
private String location;
private String language;
private String start;
private String days;
private String unit;

//省略set,get...


创建响应实体ZhiXinResponse

public class ZhiXinResponse {
private List<Results> results;
private String status;
private String status_code;
//省略set,get...
}


public class Results {
private Location location;
private List<Daily> daily;
private String last_update;

//省略set,get...

class Daily {
private String date;
private String text_day;
private String code_day;
private String text_night;
private String code_night;
private String high;
private String low;
private String precip;
private String wind_scale;

//省略set,get...
}

class Location {
private String id;
private String name;
private String country;
private String path;
private String timezone;
private String timezone_offset;

//省略set,get...
}
}


创建ZhiXinAPI,具体的接口参数规则见:http://www.thinkpage.cn/doc#now

public class ZhiXinAPI {
// 要访问的地址
private String url = "https://api.thinkpage.cn/v3";
// API密钥(虽然是免费的接口,但是暴露出来不知道会不会有问题~~)
private String authKey = "?key=9qwasjjhtafmbxz7";
// 创建默认的httpClient实例
private CloseableHttpClient client = HttpClients.custom().build();

/**
* 发送请求,接收返回信息。
* 只作为demo,忽略异常,未关闭连接
*/
public ZhiXinResponse sendRequest(ZhiXinRequest zhiXinRequest) throws ParseException, IOException {
// 组装对象
url = url + zhiXinRequest.getMethod() + authKey;
if (zhiXinRequest.getLocation() != null) {
url = url + "&location=" + zhiXinRequest.getLocation();
}
if (zhiXinRequest.getStart() != null) {
url = url + "&start=" + zhiXinRequest.getStart();
}
if (zhiXinRequest.getDays() != null) {
url = url + "&days=" + zhiXinRequest.getDays();
}
// 创建http请求(get方式)
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json-rpc");
System.out.println("httpGet: " + httpGet);
// 发送请求并接收结果(这里已经接收了到了返回结果,后面的代码只做了一些转换)
HttpResponse httpResponse = client.execute(httpGet);
String responseBody = EntityUtils.toString(httpResponse.getEntity());
ZhiXinResponse zhiXinResponse = JSON.parseObject(responseBody, ZhiXinResponse.class);
System.out.println("response: " + responseBody);
return zhiXinResponse;
}
}


创建springboot启动类Application

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


创建服务控制类ZhiXinController

@Controller
@RestController
public class ZhiXinController {

@RequestMapping("daily")
public String getWeatherDaily(String location, String days) throws ParseException, IOException {

ZhiXinRequest zhiXinRequest = new ZhiXinRequest();
// 要查询的城市
zhiXinRequest.setLocation(location);
// 要查询的天数
zhiXinRequest.setDays(days);
// 要访问的接口
String method = "/weather/daily.json";
zhiXinRequest.setMethod(method);
ZhiXinAPI zhiXinAPI = new ZhiXinAPI();
// 发送并接收返回的信息
ZhiXinResponse zhiXinResponse = zhiXinAPI.sendRequest(zhiXinRequest);
// 将信息转换成json
String result = JSON.toJSONString(zhiXinResponse);
return result;
}
}


启动服务,通过访问http://localhost:8080/daily?location=beijing&days=1 即可得到指定的天气信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: