0

我已经查看了描述HttpURLConnectionJava 中工作细节的资源,因此在 Android 中(没有使用连接池的默认实现),我的问题是:如果我关闭从 获取的流HttpURLConnection,系统会创建一个Socket下次当我建立一个相同HttpURLConnectionURL或者它会尝试使用一个现有的时?考虑以下代码:

private byte[] downloadText(URL url, String data) {
   OutputStream out = null;
   InputStream in = null;
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   try {
      conn.setRequestMethod("POST");
      conn.setReadTimeout(20 * 1000);
      conn.setConnectTimeout(15 * 000);
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

      byte[] payload = data.getBytes("utf-8");
      conn.setFixedLengthStreamingMode(payload.length);

      conn.connect();

      out = new BufferedOutputStream(conn.getOutputStream());
      out.write(payload);
      out.flush();

      final int responseCode = conn.getResponseCode();
      final String responseMessage = conn.getResponseMessage();

      is = conn.getInputStream();

      if((responseCode / 100) == 2 || responseMessage.equals("OK")) {
         return readFromStream(is);
      }
   } catch (IOException e) {
      Log.e(TAG, "Error occurred while trying to connect to the server" + e.toString());
   } finally {
      try {
         if(out != null) {
             out.close();
         }

         if(is != null) {
            is.close();
         }
      } catch(IOException e) {
         Log.e(TAG, "Error occurred while trying to close data streams" + e.toString());
      }
   }
}
4

1 回答 1

0

你的问题没有意义。如果没有连接池,则没有“现有套接字”可重用。

于 2013-03-23T01:02:23.233 回答