1.加入maven依賴
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.0</version>
</dependency>
2.基本使用
在日常開(kāi)發(fā)中最常用到的網(wǎng)絡(luò)請(qǐng)求就是GET和POST兩種請(qǐng)求方式。
HTTP GET
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
返回的是json形式的字符串
3.用jackson將進(jìn)行數(shù)組格式的json字符串轉(zhuǎn)換成List
List consulPoList =objectMapper.readValue(consulArray, new TypeReference>() {});