2012-04-16 85 views
1

關閉我當執行BufferedReader.readLine(),在下面的完整代碼的行while ((line = br.readLine()) != null) {)一個IOException。 Exception.getMessage()返回 BufferedInputStream已關閉IOException異常的BufferedInputStream在HTC

它只是發生在HTC設備,當我使用索尼愛立信XPERIA它不會發生。

我的完整代碼:

public static String downloadString(String url) throws MalformedURLException, IOException {  
    InputStream is = downloadStream(url); 

    //Convert stream into String 
    String line; 
    BufferedReader br = new BufferedReader(new InputStreamReader(is), 4096);    
    StringBuilder sb = new StringBuilder(); 
    while ((line = br.readLine()) != null) { 
     sb.append(line); 
    } 
    br.close(); 
    return sb.toString(); 
} 

public static InputStream downloadStream(String url) throws MalformedURLException, IOException { 
    return connection(url).getInputStream(); 
} 

private static HttpURLConnection connection(String s_url) throws MalformedURLException, IOException { 
    URL url = new URL(s_url); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setDoOutput(true); 
    urlConnection.connect(); 

    return urlConnection; 
} 

如何讓每一個設備上工作的更好方法downloadString?

在此先感謝。非常感謝您的回答

+0

只是爲了確保,如果你想使用「GET」請求方法,不使用urlConnection.setDoOutput(真),因爲與ICS開始,API將改變請求方法爲「POST」如果你使用setDoOutput(真)。 – 2012-04-17 17:36:47

回答

1

嘗試 -

public static String getContent(String url) throws Exception { 
     return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next()); 
} 
+0

謝謝老兄,這麼簡單的代碼!效率更高還是有什麼不同? – 2012-04-17 03:31:45

+0

希望這段代碼能幫助你,而你的問題可能會修復。 – 2012-04-17 05:56:54

1

嘗試使用HTTPGET來處理HTTP GET連接(和HttpPost處理HTTP POST)

,並確保沒有時始終關閉流需要。使用

這裏簡單的代碼HTTPGET從服務器獲取字符串:

private void executeRequest(HttpUriRequest request) 
{ 
    HttpClient client = new DefaultHttpClient(); 

    HttpResponse httpResponse; 
    try { 
     httpResponse = client.execute(request); 
     responseCode = httpResponse.getStatusLine().getStatusCode(); 
     message = httpResponse.getStatusLine().getReasonPhrase(); 

     System.out.println(responseCode + ":" +message); 

     HttpEntity entity = httpResponse.getEntity(); 

     if (entity != null) { 

      InputStream instream = entity.getContent(); 
      response = convertStreamToString(instream); 

      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 

    } catch (ClientProtocolException e) { 
     client.getConnectionManager().shutdown(); 
     Toast.makeText(null, "Error", Toast.LENGTH_LONG); 
    } catch (IOException e) { 
     client.getConnectionManager().shutdown(); 
     Toast.makeText(null, "Error", Toast.LENGTH_LONG); 
    } catch (OAuthMessageSignerException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (OAuthExpectationFailedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (OAuthCommunicationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

你可以調用該函數是這樣的:

HttpGet request = new HttpGet(url); 

executeRequest(要求);