java呼叫webservice(並不是以.wsdl結尾的)並對返回的jason資料進行解釋
阿新 • • 發佈:2019-02-02
1、返回的jason格式為:
{ "iChatAccountList": [ { "acctNo": "string", "acctStat": "string", "contNo": "string", "contStat": "string", "contractEndDate": "string", "custName": "string", "servDescChi": "string", "servDescEn": "string", "servType": "string", "subNo": "string", "totalAmntDue": "string" } ], "success": true }
2、通過HttpURLConnection拿到webservice返回的jason資料,對其進行解釋並存放在一個javabean中
public List<loginMessage> getLoginMessage(String custId, String custName) { String strMessage = ""; String servType, acctNo, contNo, acctStat, contStat, subNo, contractEndDate, totalAmntDue, custName1, servDescEn, servDescChi; HttpURLConnection conn = null; List<loginMessage> resultList = new ArrayList<loginMessage>(); try { JSONObject jsonObject = new JSONObject(); jsonObject.put("custId", custId); jsonObject.put("custName", custName); URL restUrl = new URL("http://192.168.44.24:9001/crmWS/fixedIChatLogin/login"); conn = (HttpURLConnection) restUrl.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setDoInput(true); conn.setDoOutput(true); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(jsonObject.toString()); log.info("jsonobject:" + jsonObject.toString()); dos.flush(); dos.close(); int responseCode = conn.getResponseCode(); log.info("getLoginMessage responseCode=" + responseCode); if (responseCode == 200) { BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String strOutPut = ""; StringBuilder sBuilder = new StringBuilder(); while ((strOutPut = bReader.readLine()) != null) { sBuilder.append(strOutPut); } JSONObject jObject = new JSONObject(sBuilder.toString()); String sign = jObject.getString("success"); log.info("whether the login is successful-----" + "--sign:" + sign); JSONArray jArr = jObject.getJSONArray("iChatAccountList"); for (int i = 0; i < jArr.length(); i++) { loginMessage lm = new loginMessage(); JSONObject obj2 = jArr.getJSONObject(i); servType = obj2.getString("servType"); acctNo = obj2.getString("acctNo"); contNo = obj2.getString("contNo"); acctStat = obj2.getString("acctStat"); contStat = obj2.getString("contStat"); subNo = obj2.getString("subNo"); contractEndDate = obj2.getString("contractEndDate"); totalAmntDue = obj2.getString("totalAmntDue"); custName1 = obj2.getString("custName"); servDescChi = obj2.getString("servDescChi"); System.out.print(servDescChi + "+++++++++++++++++++++++++++++++++"); servDescEn = obj2.getString("servDescEn"); lm.setSign(sign); lm.setServType(servType); lm.setAcctNo(acctNo); lm.setContNo(contNo); lm.setAcctStat(acctStat); lm.setContStat(contStat); lm.setSubNo(subNo); lm.setContractEndDate(contractEndDate); lm.setTotalAmntDue(totalAmntDue); lm.setCustName(custName1); lm.setServDescChi(servDescChi); lm.setServDescEn(servDescEn); resultList.add(lm); } } else { log.info("Loing failed"); loginMessage lm = new loginMessage(); lm.setSign("false"); resultList.add(lm); return resultList; } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } return resultList; }