2012-02-19 5 views
2

나는 안드로이드와 자바에 익숙하지만 프로그래밍에는 익숙하지 않다. (이클립스 사용하기). 메서드에서 다음과 같은 예제 코드를 실행하려고합니다.InputStream 선언

private void dummy() { 
    try { 
     URL url = new URL(quakeFeed); 
     URLConnection connection; 
     connection = url.openConnection(); 
     HttpURLConnection httpconnection = (HttpURLConnection)connection; 
     int responseCode = httpconnection.getResponseCode(); 
     if(responseCode == HttpURLConnection.HTTP_OK) 
      InputStream inp = new BufferedInputStream(httpconnection.getInputStream()); 
    } 
... 
} 

다른 모든 구문과 변수가 정의되어 있다고 가정합니다. 나는 다음과 같은 오류 얻을 : 나는 방법 밖에 InputStream를 선언하면 내가 궁금

InputStream inp; 
private void dummy() { 
    try { 
     URL url = new URL(quakeFeed); 
     URLConnection connection; 
     connection = url.openConnection(); 
     HttpURLConnection httpconnection = (HttpURLConnection)connection; 
     int responseCode = httpconnection.getResponseCode(); 
     if(responseCode == HttpURLConnection.HTTP_OK) 
     // Changed 
      inp = new BufferedInputStream(httpconnection.getInputStream()); 
    } 
    ... 
} 

즉,

InputStream` cannot be resolved to a variable.

이 이상한 경우에도 java.io.InputStream;

오류가 꺼집니다 가져 오기 후를 왜 로컬 선언 InputStream을 (를) 확인할 수 없지만 전역 선언이 해결되었습니다.

답변

5

if 다음에 성명이 올 수 있습니다. 변수를 선언하려면 블록이 필요합니다. 거기에 변수를 선언 할 수 있었다면, 아무런 가시 범위도없고 목적이 없다.

이 작동합니다 :

if(responseCode == HttpURLConnection.HTTP_OK) 
{ /* Note the brace to start a block! */ 
    InputStream inp = new BufferedInputStream(httpconnection.getInputStream()); 
    /* Now use the stream within the block. */ 
    ... 
} 
+0

좋아 덕분에 나는이 알고 않네. –