1.添加properties配置文件,位置在resources中
# 域名
#Realmname=https://rjxqymj.jczbo.com/
Realmname=http://localhost:63005/
# 获取当前用户的通道列表
UserBaseAuth=/mobile/UserBaseAuth
# 获取当前用户所选通道的二维码
UserBaseQrcode=/mobile/UserBaseQrcode
# 根据用户手机号来获取用户当前所在的企业信息
UserCompanyInfo=/mobile/UserCompanyInfo
# 根据用户企业号和手机号来获取用户当前所在的企业信息门禁二维码
UserCompanyQrcode=/mobile/UserCompanyQrcode
# 中控平台开门后插入开门记录添加开门记录(kpass)
AddDoorOpeningRecord=mobile/AddDoorOpeningRecord
# 中控平台验证二维码校验卡号(kpass)
VirifyBaseQrcode=mobile/VirifyBaseQrcode
2.添加config配置文件,位置如下
package com.jczb;
import com.jczb.mobile.utils.PropertiesUtil;
import java.lang.reflect.Field;
/**
* Author: 赵芬
* Date: 2020年8月27日17:41:33
* Remark: 后台API接口地址
*/
public class WebApiConfig {
public static String Realmname;
public static String UserBaseAuth;
public static String UserBaseQrcode;
public static String UserCompanyInfo;
public static String UserCompanyQrcode;
public static String AddDoorOpeningRecord;
public static String VirifyBaseQrcode;
static {
Field[] fieldArrays= WebApiConfig.class.getDeclaredFields();
PropertiesUtil propertiesUtil=new PropertiesUtil("webApi.properties");
for(Field field : fieldArrays){
field.setAccessible(true);
try {
field.set(WebApiConfig.class, propertiesUtil.getValue(field.getName()));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
3.添加配置文件反射类
package com.jczb.mobile.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;
/**
* Author: 吴自洋
* Date: 2020-07-26 20:45
* Remark:
*/
public class PropertiesUtil {
private Properties properties = new Properties();
public PropertiesUtil(String propertiesFileName) {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFileName);
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
try {
properties.load(bf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 依据key获取value值
*
* @author Administrator
*/
public String getValue(String key) {
return properties.getProperty(key);
}
}
4.代码调用所用的httpUtil类
package com.jczbo.centralcontrolsocket.publics.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* Author: 吴自洋
* Date: 2020-08-18 10:24
* Remark:
*/
public class HttpUtil {
//private static Logger logger = Logger.getLogger(HttpUtil.class);
/**
* get请求
* @return
*/
public static String doGet(String url) {
try {
HttpClient client = new DefaultHttpClient();
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
String strResult = EntityUtils.toString(response.getEntity());
return strResult;
}
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* post请求(用于key-value格式的参数)
* @param url
* @param params
* @return
*/
public static String doPost(String url, Map params){
BufferedReader in = null;
try {
// 定义HttpClient
HttpClient client = new DefaultHttpClient();
// 实例化HTTP方法
HttpPost request = new HttpPost();
request.setURI(new URI(url));
//设置参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String value = String.valueOf(params.get(name));
nvps.add(new BasicNameValuePair(name, value));
//System.out.println(name +"-"+value);
}
request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
if(code == 200){ //请求成功
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(),"utf-8"));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
return sb.toString();
}
else{ //
System.out.println("状态码:" + code);
return null;
}
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* post请求(用于请求json格式的参数)
* @param url
* @param params
* @return
*/
public static String doPost(String url, String params) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
else{
//logger.error("请求返回:"+state+"("+url+")");
}
}
finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
5.实际使用:
HttpUtil.doGet(WebApiConfig.AddDoorOpeningRecord + "?UserQrcode=" + data + "&Sn=" + machSn);