首先引入依赖
<!-- okhttp3 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
</dependency>
配置一个路径配置文件config.properties
//3.2查询账务
config.getRoomBill = http://****自己的地址
//4.1查询会员信息
config.checkIfVip = http://****
写一个配置类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @Auther:
* @Date: 2018/12/18 20:55
* @Description:省略getset方法
*/
@Configuration
@PropertySource(value = "classpath:config/config.properties")
public class ConfigPath {
//1.1
@Value("${config.getRoomType}")
private String getRoomType;
//1.2
@Value("${config.getRoomInfo}")
具体调用过程:
具体调用方法因人而异
String url1 = configPath.getxxx();
JSONObject map1 = new JSONObject();
map1.put("pmsTypeId", xxx.getxxx());
map1.put("hotelCode", xxx.getxxx());
map1.put("roomNo", xxx.getxxx());
map1.put("orderCode", xxx.getxxx());
str1 = OkHttpUtil.doPostHttpRequest(url1, map1.toString());
负责发送postHTTP请求的配置:
package com.chinahotelhelp.shm.businessmanagement.tools;
import com.squareup.okhttp.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @Auther:
* @Date: 2018/12/14 11:47
* @Description:
*/
public class OkHttpUtil {
private static final OkHttpClient client = new OkHttpClient();
public static final MediaType JSON = MediaType
.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_PNG = MediaType
.parse("image/png;charset=utf-8");
private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType
.parse("text/x-markdown; charset=utf-8");
static {
client.setConnectTimeout(30, TimeUnit.SECONDS);
}
/**
* 不会开启异步线程。
*
* @param request
* @return
* @throws IOException
*/
public static Response execute(Request request) throws IOException {
return client.newCall(request).execute();
}
/**
* 开启异步线程访问网络
*
* @param request
* @param responseCallback
*/
public static void enqueue(Request request, Callback responseCallback) {
client.newCall(request).enqueue(responseCallback);
}
/**
* 根据url地址获取数据
*
* @param url
* @return
* @throws IOException
*/
public static String doGetHttpRequest(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
/**
* 根据url地址和json数据获取数据
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static String doPostHttpRequest(String url, String json)
throws IOException {
Request request = new Request.Builder().url(url)
.post(RequestBody.create(JSON, json)).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
/**
* 根据url地址和json数据获取数据
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static String doPostHttpRequest2(String url, String json)
throws IOException {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder().url(url).post(body)
.addHeader("content-type", "application/json").build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
public static String doPostImgHttpRequest(String url, File file)
throws IOException {
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("buffer", file.getName(),
RequestBody.create(MEDIA_TYPE_PNG, file)).build();
Request request = new Request.Builder().url(url).post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
}