// 構(gòu)建報(bào)文
JSONObject rootJson = new JSONObject();
// 賦值
rootJson.put("unique_code", qniqueCode);
JSONObject dataJson = null;
JSONArray dataList = new JSONArray();
for (TransferFundResult rfr : rfrResultList) {
for (Map.Entry<SubjectEnum, TransferFundItem> entry : rfr.entrySet()) {
entry.getValue();
// receive_number ★收款會員號
dataJson = new JSONObject();
dataJson.put("receive_number", rfr.getTransferMainbodyOrgMemberId());
// pay_type ★付款類別
dataJson.put("pay_type", rftValId.get(entry.getKey().getValue()) + "");
// amount 金額
dataJson.put("amount", entry.getValue().getAmount().toString() + "");
// group_state ★支付狀態(tài)
dataJson.put("group_state", entry.getValue().getAllocationResult().getValue() + "");
// group_error_info ★錯(cuò)誤信息
dataJson.put("group_error_info", entry.getValue().getResult());
dataList.add(dataJson);
}
}
rootJson.put("data_list", dataList);
/***************************************************
* @Description 使用http協(xié)議發(fā)送字節(jié)流消息
* @Date 2019-10-22 10:13
* @param hostIp 遠(yuǎn)程主機(jī)IP地址
* @param port 遠(yuǎn)程主機(jī)端口
* @param path 遠(yuǎn)程主機(jī)服務(wù)路徑
* @param method 提交方式
* @param sendBytes 發(fā)送的字節(jié)
* @return
* @throws IOException byte[]
***************************************************/
public static byte[] sendHttpBytesMessage(String hostIp, int port, String path, String method, byte[] sendBytes)
throws IOException {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
String urlStr = "http://" + hostIp + ":" + port + "/" + path;
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "text/xml");
httpURLConnection.setRequestMethod(method.toUpperCase());
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
bos = new BufferedOutputStream(httpURLConnection.getOutputStream());
bos.write(sendBytes);
bos.flush();
bos.close();
bis = new BufferedInputStream(httpURLConnection.getInputStream());
int responseCode = httpURLConnection.getResponseCode();
byte[] temp = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
if (responseCode == httpURLConnection.HTTP_OK) {
while ((len = bis.read(temp)) > 0) {
baos.write(temp, 0, len);
}
} else {
System.out.println("請求響應(yīng)返回碼=" + responseCode);
}
return baos.toByteArray();
}
// 接收響應(yīng)
byte[] response = null;
try {
response = ConmunicationUtil.sendHttpBytesMessage("192.168.1.1", 8080, "lpapiLYPH0006.action",
"post", reqMsg.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("response =>> " + new String(response, "utf-8"));
// 對端接收解析
// 以流的方式獲取請求報(bào)文內(nèi)容
String reqMsg = null;
System.out.println("contentLength =" + this.request.getContentLength());
byte[] tempContentBytes = new byte[4096];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean flag = true;
try {
BufferedInputStream bis = new BufferedInputStream(this.request.getInputStream());
while ((len = bis.read(tempContentBytes)) >= 0) {
System.out.println("tempContentBytes=" + Arrays.toString(tempContentBytes));
baos.write(tempContentBytes, 0, len);
}
reqMsg = baos.toString("utf-8");
log.debug("接受的請求字節(jié)碼=" + Arrays.toString(baos.toByteArray()));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
flag = false;
responseMsg = settlementResult.getFailedMesageTemplate(999, "字符編碼處理異常-" + e1.getMessage());
log.error("請求報(bào)文處理異常", e1);
} catch (IOException e1) {
e1.printStackTrace();
flag = false;
responseMsg = settlementResult.getFailedMesageTemplate(999, "I/O處理異常-" + e1.getMessage());
log.error("請求報(bào)文處理異常", e1);
} finally {
if (!flag) {
this.sendResponseMessage(responseMsg, "UTF-8");
}
}
JSONObject json = null;
// 流轉(zhuǎn)換JSON
json = JSONObject.fromObject(reqMsg);
// JsonPath取值
String logisticMemberCode = JsonPath.read(json, "$.transit_code");
JSONArray dayList = JsonPath.read(json, "$.data_list");
for (int i = 0; i < dayList.size(); i++) {
JSONObject job = dayList.getJSONObject(i);
payType = Integer.parseInt((String) JsonPath.read(job, "$.pay_type"));
}
JSON報(bào)文創(chuàng)建與接收 JSONObject or JSONArray
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人伐厌,你說我怎么就攤上這事承绸。” “怎么了挣轨?”我有些...
- 文/不壞的土叔 我叫張陵军熏,是天一觀的道長。 經(jīng)常有香客問我卷扮,道長荡澎,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任晤锹,我火速辦了婚禮摩幔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鞭铆。我一直安慰自己或衡,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布车遂。 她就那樣靜靜地躺著封断,像睡著了一般。 火紅的嫁衣襯著肌膚如雪舶担。 梳的紋絲不亂的頭發(fā)上坡疼,一...
- 文/蒼蘭香墨 我猛地睜開眼拯欧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了财骨?” 一聲冷哼從身側(cè)響起镐作,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎隆箩,沒想到半個(gè)月后该贾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡捌臊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年杨蛋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站揩抡,受9級特大地震影響户侥,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜峦嗤,卻給世界環(huán)境...
- 文/蒙蒙 一添祸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寻仗,春花似錦刃泌、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至曹体,卻和暖如春俗扇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背箕别。 一陣腳步聲響...
- 正文 我出身青樓除抛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親母截。 傳聞我的和親對象是個(gè)殘疾皇子到忽,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處清寇。 依賴 一喘漏、應(yīng)用場景 在編寫 jav...
- 公司項(xiàng)目原因,接觸了一下視頻流H264的編解碼知識华烟,之前項(xiàng)目使用的是FFMpeg多媒體庫翩迈,利用CPU做視頻的編碼和...