InputStreamReader java.io.IOException:Stream已关闭

时间:2013-11-28 17:32:16

标签: android inputstreamreader

我正在编写一个连接到服务器的应用程序,并检索有关涡轮机的数据。 在代码中我打开两个HTTP连接。第一个工作,第二个抛出这个错误。 这是带错误的代码:

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)

使用Homo 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)

解答: 对不起,所有麻烦,事实证明我错误的URL。我的坏。

1 个答案:

答案 0 :(得分:2)

您已在“

中关闭InputStream

替换

isr = new InputStreamReader(in);

isr = new InputStreamReader(in2);