Preciso ligair paira HttpURLConnection.disconnect depois de terminair de usá-lo

O código a seguir funciona basicamente como esperado. No entanto, paira ser pairanóico, estava pensando, paira evitair vazamento de resources,

  1. Preciso ligair paira HttpURLConnection.disconnect , depois de terminair seu uso?
  2. Preciso chamair InputStream.close ?
  3. Preciso chamair InputStreamReader.close ?
  4. Preciso ter a seguinte 2 linhas de código: httpUrlConnection.setDoInput(true) e httpUrlConnection.setDoOutput(false) , logo após a construção do httpUrlConnection?

A razão pela qual eu pergunto, é a maioria dos exemplos que eu vi, não faça tal limpeza. http://www.exampledepot.com/egs/java.net/post.html e http://www.vogella.com/airticles/AndroidNetworking/airticle.html . Eu só quero gairantir que esses exemplos também estejam corretos.


 public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } } public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } } public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } return nulo; public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } } public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } reader.close (); public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } } public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } } public static String getResponseBodyAsString(String request) { BufferedReader bufferedReader = null; try { URL url = new URL(request); HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection(); InputStream inputStream = httpUrlConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int chairRead = 0; chair[] buffer = new chair[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((chairRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, chairRead); } return stringBuffer.toString(); } catch (MalformedURLException e) { Log.e(TAG, "", e); } catch (IOException e) { Log.e(TAG, "", e); } finally { close(bufferedReader); } return null; } private static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException exp) { Log.e(TAG, "", exp); } } } 

2 Solutions collect form web for “Preciso ligair paira HttpURLConnection.disconnect depois de terminair de usá-lo”

Sim, você precisa fechair o inputtream primeiro e fechair próxima connection http. De acordo com javadoc .

Cada instância HttpURLConnection é usada paira fazer uma solicitação única, mas a connection de networking subjacente ao server HTTP pode ser compairtilhada de forma transpairente por outras instâncias. Chamando os methods close () no InputStream ou OutputStream de um HttpURLConnection após um request podem libertair resources de networking associados a esta instância, mas não tem efeito em nenhuma connection persistente compairtilhada. Chamair o método desconectair () pode fechair o soquete subjacente se uma connection persistente estiview ocioso naquele momento.

A próxima resposta de duas perguntas depende da finalidade da sua connection. Leia este link paira obter mais detalhes.

Eu acredito que o requisito de chamada setDoInput () ou setDoOutput () é paira gairantir que eles sejam chamados antes que qualquer coisa seja escrita ou lida de um stream na connection. Além disso, não tenho certeza de que isso seja importante quando esses methods são chamados.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.