webview - Should I close HttpURLConnection in android? -


i override following function cache myself (to bypass networking lookup)

public webresourceresponse shouldinterceptrequest(webview view, string url) {     // depending on android version, use below function     // public webresourceresponse shouldinterceptrequest(webview view, webresourcerequest request) {      httpurlconnection urlconnection = null;     try {         urlconnection = (httpurlconnection) url.openconnection();          in = new bufferedinputstream(urlconnection.getinputstream());         data = new string(bytestreams.tobytearray(in));      } catch (exception e) {         return null;     }      // stuff data omitted here      return new webresourceresponse(mimetype, "utf-8", in);  } 

i'm not closing urlconnection because i'm passing inputstream tied urlconnection.

since it's common idiom if open something, should close it, i'm worried if i'm ok code.

do need call httpurlconnection.disconnect after finish using it discusses it, couldn't definitive answer there whether allowed not close it.

once response body has been read, httpurlconnection should closed calling disconnect(). disconnecting releases resources held connection may closed or reused.

url url = new url("http://www.android.com/");    httpurlconnection urlconnection = (httpurlconnection) url.openconnection();    try {      inputstream in = new bufferedinputstream(urlconnection.getinputstream());      readstream(in);     {      urlconnection.disconnect();    }  } 

you can read uses of class follow pattern here

edit: here more


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -