Note that there are some explanatory texts on larger screens.

plurals
  1. POInputStreamReader java.io.IOException: Stream is closed
    primarykey
    data
    text
    <p>I'm coding an app that connects to a server and retrieves data about turbines. In the code I open two HTTP connections. the first one works, the seccond one throws this error. Here's the code with the error:</p> <pre><code>private void downloadText(String urlStr) { final String url = urlStr; new Thread(new Runnable() { public void run() { int BUFFER_SIZE = 2000; InputStream in = null; try { in = openHttpConnection(url); int charRead; text = ""; char[] inputBuffer = new char[BUFFER_SIZE]; while ((charRead = isr.read(inputBuffer))&gt;0) { String readString = String.copyValueOf(inputBuffer, 0, charRead); text += readString; inputBuffer = new char[BUFFER_SIZE]; } Bundle b = new Bundle(); b.putString("text", text); fullTurbineList = text; viewData.getSetData(true, fullTurbineList); in.close(); } catch (IOException e) { e.printStackTrace(); } try { JSONArray jsonArray = new JSONArray(fullTurbineList); if(jsonArray != null) { ids = new String[jsonArray.length()]; names = new String[jsonArray.length()]; actives = new boolean[jsonArray.length()]; for(int i = 0 ; i &lt; jsonArray.length() ; i++) { JSONObject jsonResult = jsonArray.getJSONObject(i); ids[i] = jsonResult.optString("turbine_id"); viewData.getSetData(true, jsonArray.toString()); //if (jsonResult.opt("turbine_id") == null) { // viewData.getSetData(true, "ids[i] is null... ?????"); //} names[i] = jsonResult.optString("name"); actives[i] = jsonResult.optBoolean("active"); } String toSet = ""; int count = 0; for (String hello: ids) { toSet += "\nname: " + names[count] + "\nid: " + hello + "\n"; count ++; } viewData.getSetData(true, toSet); } } catch (Exception e) { e.printStackTrace(); } //} else { int BUFFER_SIZE2 = 2000; InputStream in2 = null; try { in2 = openHttpConnection("http://stafford.scaledenergy.co.uk/endurancelogging/index/id/" + ids[0]);//10017510 isr = new InputStreamReader(in); // ERROR HERE int charRead; text = ""; char[] inputBuffer = new char[BUFFER_SIZE2]; //if (!isr.equals(null)) { while ((charRead = isr.read(inputBuffer))&gt;0) //ERROR FOUND BY COMPILER HERE { String readString = String.copyValueOf(inputBuffer, 0, charRead); text += readString; inputBuffer = new char[BUFFER_SIZE]; } Bundle b = new Bundle(); b.putString("text", text); turbineData = text; in2.close(); isr.close(); //} else { // viewData.getSetData(true, "ISR IS NULL"); //} } catch (IOException e) { e.printStackTrace(); } try { JSONObject jsonResult = new JSONObject(turbineData); if(jsonResult != null) { turbineData = "name: " + names[0] + " shutdown: " + jsonResult.getBoolean("shutdown"); //viewData.getSetData(true, turbineData); } } catch (Exception e){ e.printStackTrace(); } //} //messageHandler.sendMessage(msg); } //end run() }).start(); </code></pre> <p>I can't figure it out. I've looked for days on the web to no avail. Am I missing something?</p> <p>EDIT: Heres the openHttpConnection method code as well:</p> <pre><code>public InputStream openHttpConnection(String urlStr) { InputStream in = null; int resCode = -1; try { URL url = new URL(urlStr); URLConnection urlConn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection)urlConn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); resCode = httpConn.getResponseCode(); if (resCode == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return in; } </code></pre> <p>EDIT: Stack trace:</p> <pre><code>java.io.IOException: Stream is closed at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:244) at java.io.Reader.read(Reader.java:145) at com.firstapp.bensapp.TheNextActivity$1.run(TheNextActivity.java:257) at java.lang.Thread.run(Thread.java:856) </code></pre> <p>After using Homo apiens advice I get this:</p> <pre><code>FATAL EXCEPTION: Thread-546 java.lang.NullPointerException at java.io.Reader.&lt;init&gt;(Reader.java:64) at java.io.InputStreamReader.&lt;init&gt;(InputStreamReader.java:79) at com.firstapp.bensapp.TheNextActivity$1.run(TheNextActivity.java:252) at java.lang.Thread.run(Thread.java:856) </code></pre> <p>ANSWER: Sorry for all the bother, it turns out I got the URL wrong. My bad.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload