2013-11-28 70 views
0

我編寫的應用程序連接到服務器並檢索有關渦輪機的數據。 在代碼中,我打開兩個HTTP連接。第一個工作,第二個拋出這個錯誤。 這裏是有錯誤的代碼:InputStreamReader java.io.IOException:流關閉

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))>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 < 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))>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(); 

我無法弄清楚。我在網上搜索了幾天無濟於事。我錯過了什麼嗎?

編輯: 繼承人的openHttpConnection方法的代碼,以及:

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; 
    } 

編輯:堆棧跟蹤:

使用智人
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) 

後apiens的意見,我得到這個:

FATAL EXCEPTION: Thread-546 
java.lang.NullPointerException 
at java.io.Reader.<init>(Reader.java:64) 
at java.io.InputStreamReader.<init>(InputStreamReader.java:79) 
at com.firstapp.bensapp.TheNextActivity$1.run(TheNextActivity.java:252) 
at java.lang.Thread.run(Thread.java:856) 

答案: 對不起所有的麻煩,它變成了o我得到了錯誤的網址。我的錯。

回答

2

你已經「在」

關閉InputStream更換

isr = new InputStreamReader(in); 

isr = new InputStreamReader(in2); 
+0

這曾在它擺脫了錯誤的,但現在我在得到一個空指針異常InputStream in2。 – dodo

+0

可以請你分享你的完整打印堆棧跟蹤。 – Subbu

+0

請在使用之前檢查您的輸入流是否已打開並且不爲空。 – Subbu