2013-11-25 44 views
2

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

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): [email protected] 
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): [email protected] 
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 
+0

请阅读以下博客。从url获取数据http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ – prakash

+0

手机可以只用xml或json – prakash

回答

3

在方法convertString你关闭阅读器 - reader.close();关闭strea米

0

试试这个

删除此行

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

然后它也显示我同样的错误 – Ashishsingh

相关问题