static void setRequestMethod(HttpURLConnection urlConnection, String method) {
   try {
     urlConnection.setRequestMethod(method);
     if (method.equalsIgnoreCase(POST_METHOD) || method.equalsIgnoreCase(PUT_METHOD)) {
       urlConnection.setDoOutput(true);
     }
   } catch (ProtocolException e) {
     Log.e("URLConnection exception", e.toString());
   }
 }
예제 #2
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;
  }
  @Override
  protected Integer doInBackground(Object[] params) {
    try {
      // Opretter POST URL
      try {
        String urlAPI = null;
        urlAPI =
            context.getString(R.string.API_URL_MATHIAS) + itemid + "?userID=56837dedd2d76438906140";
        url = new URL(urlAPI);
        System.out.println("URL til at uploade lyd: " + url);
      } catch (MalformedURLException e) {
        e.printStackTrace();
      }

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "audio/3gp"); // content type til Mathias' API

      //            String filePath = String.valueOf(prefs.getString("chosenImage", null));
      prefs = PreferenceManager.getDefaultSharedPreferences(context);
      String recordingPath = prefs.getString("recording", null);
      Log.d(TAG, "recordingPath: " + recordingPath);

      OutputStream os = conn.getOutputStream();
      FileInputStream inputStream = new FileInputStream(recordingPath);

      byte[] data = new byte[1024];
      int read;

      while ((read = inputStream.read(data)) != -1) {
        os.write(data, 0, read);
      }
      inputStream.close();
      os.flush();
      os.close();

      responseCode = conn.getResponseCode();
      String responseMsg = "PostHTTPController.java - Response Code: " + responseCode;
      Log.d(TAG, responseMsg);

      // Evt. læse svaret men ved ikke om vi har brug for andet end response code?
      StringBuffer response = new StringBuffer();
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      Log.d(TAG, response.toString());

      in.close();
      conn.disconnect();

    } catch (UnsupportedEncodingException e) {
      Log.d(TAG, e.toString());
    } catch (ProtocolException e) {
      Log.d(TAG, e.toString());
    } catch (IOException e) {
      Log.d(TAG, e.toString());
    }
    return responseCode;
  }