2013-11-28 68 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