收藏
回答

java 发货信息录入接口字段item_desc中文乱码怎么解决?

文档地址:

https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E4%B8%80%E3%80%81%E5%8F%91%E8%B4%A7%E4%BF%A1%E6%81%AF%E5%BD%95%E5%85%A5%E6%8E%A5%E5%8F%A3

请求参数:

{
    "shipping_list": [
        {
            "item_desc": "书籍"
        }
    ],
    "upload_time": "2023-10-23T16:21:27.144+08:00",
    "order_key": {
        "transaction_id": "4200001985202310235326041813",
        "order_number_type": 2
    },
    "logistics_type": 4,
    "payer": {
        "openid": "oMZf_5DPXWNH0cHxIKcEISo56"
    },
    "delivery_mode": 1
}


发起POST请求:

HttpPost httpPost = new HttpPost(httpUrl);
//以json形式提交请求
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");


返回状态正常,已经发货成功,但是在微信客户端服务通知中商品信息乱码,大佬么是怎么解决的?





回答关注问题邀请回答
收藏

2 个回答

  • CRMEB
    CRMEB
    2023-10-23

    示例代码:

    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    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.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    
    public class Main {
        public static void main(String[] args) {
            String httpUrl = "http://example.com/api";
            String json = "{\"shipping_list\": [{\"item_desc\": \"书籍\"}],\"upload_time\": \"2023-10-23T16:21:27.144+08:00\",\"order_key\": {\"transaction_id\": \"4200001985202310235326041813\",\"order_number_type\": 2},\"logistics_type\": 4,\"payer\": {\"openid\": \"oMZf_5DPXWNH0cHxIKcEISo56\"},\"delivery_mode\": 1}";
    
    
            try {
                json = URLEncoder.encode(json, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
    
            HttpPost httpPost = new HttpPost(httpUrl);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.setEntity(new StringEntity(json, "UTF-8"));
    
    
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                CloseableHttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String result = EntityUtils.toString(entity, "UTF-8");
                    System.out.println(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
    2023-10-23
    有用 1
    回复 5
    • 别离
      别离
      2023-10-24
      还是没有看出来问题在哪?
      2023-10-24
      回复
    • 别离
      别离
      2023-10-24
      public static JSONObject doPost(String httpUrl, Map<String, String> header, Object data, int timeout) {
              CloseableHttpClient client = null;
              CloseableHttpResponse response = null;
              JSONObject result = null;
              try {
                  //创建可关闭的 HttpClient 实例
                  client = HttpClients.createDefault();
                  //创建 HttpPost 实例并设置请求头
                  HttpPost httpPost = new HttpPost(httpUrl);
                  //以json形式提交请求
                  httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
                  if (Objects.nonNull(header) && header.size() > 0) {
                      Set<Map.Entry<String, String>> entries = header.entrySet();
                      for (Map.Entry<String, String> entry : entries) {
                          httpPost.setHeader(entry.getKey(), entry.getValue());
                      }
                  }
                  //设置请求参数和参数格式
                  StringEntity entity = new StringEntity(JSONObject.toJSONString(data));
                  entity.setContentType("application/json;charset=UTF-8");
                  httpPost.setEntity(entity);
                  //请求配置项
                  RequestConfig config = RequestConfig.custom()
                          //设置连接超时时间
                          .setConnectTimeout(timeout <= 0 ? 30000 : timeout)
                          //设置传输超时时间
                          .setSocketTimeout(timeout <= 0 ? 30000 : timeout)
                          .build();
                  httpPost.setConfig(config);
                  //执行请求
                  response = client.execute(httpPost);
                  if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                      HttpEntity httpEntity = response.getEntity();
                      if (httpEntity!=null){
                          String s = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                          result = JSONObject.parseObject(s);
                      }else {
                          //如果entity为空,那么直接消化掉即可
                          EntityUtils.consume(httpEntity);
                      }
                  }
              } catch (IOException e) {
                  log.error("发送post请求错误,请求URL:{},请求参数:{},错误信息:{}", httpUrl, data, e.getMessage());
              } finally {
                  //释放链接
                  releaseConnection(client, response);
              }
              return result;
          }
      2023-10-24
      回复
    • CRMEB
      CRMEB
      2023-10-24回复别离
      尝试在创建HttpPost实例时设置请求头的字符编码为UTF-8
      2023-10-24
      回复
    • 别离
      别离
      2023-10-24
      可是我这里的请求头 和参数都设置json了呀
      2023-10-24
      回复
    • 别离
      别离
      2023-10-24
      谢谢大佬,完美解决
      2023-10-24
      回复
  • 宋〇〇
    宋〇〇
    04-26

    json化的时候转义了,用这个

    $str = json_encode($a, JSON_UNESCAPED_UNICODE);


    04-26
    有用
    回复
登录 后发表内容