Пример #1
0
  /**
   * 查询知识库讨论主题
   *
   * @param paraMap 输入参数所有对象封装在Map
   * @return 输出参数所有对象封装在Map
   * @throws SysException
   * @throws AppException
   */
  public Map findKbTopic4REST(Map paraMap) throws SysException, AppException {
    Map resultMap = new HashMap();
    try {
      // String urlStr = SysConfigData.getSysConfigCurValue(
      //	SysConstants.SYS_CONFIG_SMS_SERVER_ADDR, null, null, null,
      // null, null);
      paraMap.put("module", "topicApi");
      paraMap.put("action", "list");
      paraMap.put("api_key", "userapi");
      String paramStr = prepareParam(paraMap);
      String urlStr =
          SysConfigData.getSysConfigCurValue(
              SysConstants.SYS_CONFIG_MOS_JFORUM_ADDR, null, null, null, null, null);
      urlStr = urlStr + "topicApi/list.page";
      urlStr = urlStr + "?" + paramStr;
      URL url = new URL(urlStr);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      // 设置发送请求的方式
      conn.setRequestMethod("POST");
      // 设置参数的格式类型
      conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
      // 打开输入输出,在output中传输参数
      conn.setDoInput(true);
      conn.setDoOutput(true);
      OutputStream os = conn.getOutputStream();
      os.close();
      // 正常时返回的状态码为200
      if (conn.getResponseCode() == 200) {
        // 获取返回的内容
        BufferedReader br =
            new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        // 输出返回的信息
        String line;
        StringBuffer contentBuffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
          System.out.println(line);
          contentBuffer.append(line);
        }
        System.out.print(contentBuffer.toString());
        br.close();
        Document doc = null;
        List topicList = new ArrayList();
        // 下面的是通过解析xml字符串的
        doc = DocumentHelper.parseText(contentBuffer.toString()); // 将字符串转为XML
        Element rootElt = doc.getRootElement(); // 获取根节点
        Element forumsElement = rootElt.element("topics");
        Iterator topicItr = forumsElement.elementIterator("topic"); // 获取topic节点列表
        // 遍历forum节点
        while (topicItr.hasNext()) {
          Element topicElt = (Element) topicItr.next();
          KbTopicSVO kbTopicSVO = new KbTopicSVO();
          kbTopicSVO.setTopicId(topicElt.attributeValue("id"));
          kbTopicSVO.setTopicTitle(topicElt.attributeValue("title"));
          kbTopicSVO.setTopicDesc(topicElt.getText());
          kbTopicSVO.setTopicReplies(Integer.parseInt(topicElt.attributeValue("replies")));
          kbTopicSVO.setPostUserName(topicElt.attributeValue("postUserName"));
          kbTopicSVO.setTopicTime(
              DateUtil.to_date(topicElt.attributeValue("firstPostTime"), DATE_FORMAT));
          kbTopicSVO.setFirstPostTime(DateUtil.date2Str(kbTopicSVO.getTopicTime()));
          topicList.add(kbTopicSVO);
        }
        resultMap.put("topicList", topicList);
      } else {
        log.info("POST" + " [ERROR] CODE: " + conn.getResponseCode());
      }

    } catch (ProtocolException ptclEx) {
      log.info(ptclEx.toString());
      throw new AppException("", "调用RESTful接口时协议异常!");
    } catch (IOException ioEx) {
      log.info(ioEx.toString());
      throw new AppException("", "方法体内进行输入输出操作时异常!");
    } catch (DocumentException docEx) {
      log.info(docEx.toString());
      throw new AppException("", "解析调用RESTful接口返回参数时异常!");
    }
    return resultMap;
  }