2012-04-16 4 views
1

아래의 전체 코드에서 BufferedReader.readLine()을 실행하면 IOException이 발생합니다 (while ((line = br.readLine()) != null) {). Exception.getMessage()을 반환합니다. BufferedInputStream은으로 닫힙니다.IOException BufferedInputStream이 HTC에서 닫혔습니다.

소니 에릭슨 XPERIA를 사용하면 HTC 장치에서만 발생합니다.

내 전체 코드 :

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'요청 메소드를 사용하려면 ICS로 시작하므로 urlConnection.setDoOutput (true)을 사용하지 말고 setDoOutput (true)을 사용하면 API가 요청 메소드를 'POST'로 변경합니다.). –

답변

1

그것을 시도 -

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

고마워, 그런 간단한 코드! 더 효율적인가, 아니면 차이점이 무엇인가 ..? –

+0

이 코드가 도움이되기를 바랍니다. 그러면 문제가 해결 될 수 있습니다. –

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 (요청);