java.io.ioexception尝试在封闭流上读取

时间:2013-11-25 12:36:13

标签: android

我是android world的新手。我正在尝试了解客户端和服务器连接,所以,我写了一个程序,但它显示我 java.io.ioexception尝试在封闭流上读取当我打印转换后的respose时,在我的Logat中它向我显示整个html页面。这是我的代码

private class getDataFromDatabse extends AsyncTask<String, String, String> {

        boolean switchintent=false;
        String busStopNme;
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(WelcomeActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected String doInBackground(String... params) {
            try {
                // TODO Auto-generated method stub
                HttpClient httpclient = new DefaultHttpClient();
                System.out.println("IP is " + IP);
                HttpPost httppost = new HttpPost("http://" + IP + "/connection.php");
                /*List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("BusStopName","thakur_complex"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));*/
                System.out.println(httppost);
                Log.e("welocmeActivity", "HTTPPost is " + httppost.getURI());
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                System.out.println("top of convert string");
                String tempValue = convertString(is);
                System.out.println(tempValue);
                if (convertString(is) == "succeed") {

                    busStopNme = "Thakur Complex";
                    switchintent=true;

                }
                System.out.println(is.toString());
                System.out.println("bottom of convert string");
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("Exception" +e);
                e.printStackTrace();
            }

            return busStopNme;
        }

        private String convertString(InputStream is) {

            BufferedReader reader;
            try {
                reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);

                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                reader.close();
                busStopNme = sb.toString();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return busStopNme;
        }

我的logcat文件如下所示..

1-25 17:55:02.473: I/System.out(383): IP is **********
11-25 17:55:02.473: I/System.out(383): org.apache.http.client.methods.HttpPost@4a32c610
11-25 17:55:02.473: E/welocmeActivity(383): HTTPPost is http://**********/connection.php
11-25 17:55:02.763: I/System.out(383): above tts in init
11-25 17:55:02.763: I/TTS received:(383): Welcome to Audio Bus , Please wait we are retriving information
11-25 17:55:02.773: I/System.out(383): below tts in init
11-25 17:55:04.373: I/System.out(383): top of convert string
11-25 17:55:04.383: I/System.out(383): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
11-25 17:55:04.383: I/System.out(383): <html><head>
11-25 17:55:04.383: I/System.out(383): <title>403 Forbidden</title>
11-25 17:55:04.383: I/System.out(383): </head><body>
11-25 17:55:04.403: I/System.out(383): <h1>Forbidden</h1>
11-25 17:55:04.403: I/System.out(383): <p>You don't have permission to access /connection.php
11-25 17:55:04.403: I/System.out(383): on this server.</p>
11-25 17:55:04.403: I/System.out(383): </body></html>
11-25 17:55:04.403: W/System.err(383): java.io.IOException: Attempted read on closed stream.
11-25 17:55:04.483: D/dalvikvm(383): GC_FOR_MALLOC freed 2856 objects / 168704 bytes in 73ms
11-25 17:55:04.483: W/System.err(383):  at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:127)
11-25 17:55:04.483: W/System.err(383):  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:157)
11-25 17:55:04.483: W/System.err(383):  at java.io.InputStreamReader.read(InputStreamReader.java:275)
11-25 17:55:04.483: W/System.err(383):  at java.io.BufferedReader.fillBuf(BufferedReader.java:155)
11-25 17:55:04.483: W/System.err(383):  at java.io.BufferedReader.readLine(BufferedReader.java:386)
11-25 17:55:04.483: W/System.err(383):  at com.example.audiobus.WelcomeActivity$getDataFromDatabse.convertString(WelcomeActivity.java:157)
11-25 17:55:04.483: W/System.err(383):  at com.example.audiobus.WelcomeActivity$getDataFromDatabse.doInBackground(WelcomeActivity.java:133)
11-25 17:55:04.483: W/System.err(383):  at com.example.audiobus.WelcomeActivity$getDataFromDatabse.doInBackground(WelcomeActivity.java:1)
11-25 17:55:04.483: W/System.err(383):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-25 17:55:04.483: W/System.err(383):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-25 17:55:04.483: W/System.err(383):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-25 17:55:04.483: W/System.err(383):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-25 17:55:04.483: W/System.err(383):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-25 17:55:04.483: W/System.err(383):  at java.lang.Thread.run(Thread.java:1096)
11-25 17:55:04.483: I/System.out(383): org.apache.http.conn.EofSensorInputStream@4a372020
11-25 17:55:04.483: I/System.out(383): bottom of convert string
11-25 17:55:04.493: I/TTS received:(383): Connection is not establish, Please restart the Application

2 个答案:

答案 0 :(得分:3)

在方法 convertString 中关闭阅读器 - reader.close();,关闭流。

答案 1 :(得分:0)

试试这个

删除此行

String tempValue = convertString(is);
System.out.println(tempValue);
System.out.println(is.toString());