2011-09-16 2 views
0

아래 코드는 Android 에뮬레이터에서는 작동하지만 실제 장치에서는 작동하지 않습니다. inputstream.available()은 에뮬레이터에서 장치에 -1을 반환하면 int 값을 수천 단위로 반환합니다. 어떤 도움이 필요합니까?android의 데이터 연결성 : InputStream의`available`이 다른 값을 반환합니다.

코드 :

try { 
       URL url = new URL("http://www.google.com"); 
       Log.d(TAG,"Set URL"); 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       //urlConnection.connect(); 
       Log.d(TAG,"Open connection"); 
       InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
       Log.d(TAG,"get input"); 
       str="Connect time out::"+urlConnection.getConnectTimeout()+ 
        "\nContent length:"+urlConnection.getContentLength()+ 
        "\nRead time out::"+ urlConnection.getReadTimeout()+ 
        "\nResponse messgae::"+urlConnection.getResponseMessage()+ 
        "\nAvailable bytes::::"+in.available(); 
       Log.d(TAG,""+str); 
       read(in); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       urlConnection.disconnect(); 
      } 

답변

2

Genereally 당신이 HttpClient를 안드로이드가 제공하는 사용한다, 자바 URL 클래스는 충분히 강력하지 않다.

같은 :

HttpClient client = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(); 
    request.setURI(new URI("http://w3mentor.com/")); 
    HttpResponse response = client.execute(request); 
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
관련 문제