jk-GitHub-coder 4 éve
szülő
commit
a582296750

+ 14 - 13
YijiaRestful/src/main/java/com/platform/yijia/utils/PosPrinterUtil.java

@@ -1,9 +1,12 @@
 package com.platform.yijia.utils;
 
+import com.alibaba.fastjson.JSONObject;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicNameValuePair;
@@ -13,10 +16,7 @@ import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * <Title> PosPrinterUtil </Title>
@@ -38,8 +38,8 @@ public class PosPrinterUtil {
 
     //推送要打印小票的信息
     public static void sendPosPrintDirectMessage(Map<String, Object> args){
-
-        Map<String, Object> params = new HashMap<>();
+        logger.info("开始推送消息:" +args.toString());
+        Map<String, Object> params = new Hashtable<>();
         params.put("messageTitle", "pos");
         params.put("messageType", "sys");
         List<Map<String, Object>> list = new ArrayList<>();
@@ -47,20 +47,21 @@ public class PosPrinterUtil {
         Map<String, Object> map = new HashMap<>();
         map.put("contentData", list);
         params.put("messageContent", map);
-
+        logger.info("开始推送消息:" +JSONObject.toJSONString(params));
         //创建请求
         CloseableHttpClient httpClients = HttpClients.createDefault();
         CloseableHttpResponse response = null;
         try {
+
+            // 创建Http Post请求
             HttpPost httpPost = new HttpPost(SEND_POS_PRINT_MESSAGE_URL);
-            List<NameValuePair> nameValuePairList = new ArrayList<>();
-            for (String key : params.keySet()){
-                nameValuePairList.add(new BasicNameValuePair(key, params.get(key).toString()));
-            }
-            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
-            httpPost.setEntity(urlEncodedFormEntity);
+            // 创建请求内容
+            StringEntity entity = new StringEntity(JSONObject.toJSONString(params), ContentType.APPLICATION_JSON);
+            httpPost.setEntity(entity);
+            // 执行http请求
             response = httpClients.execute(httpPost);
             String resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+
             logger.info("POS推送订单消息结果:" +resultString);
         } catch (Exception e) {
             e.printStackTrace();