2013-04-29 3 views
0

URL에 연결하기위한 다음 코드가 있습니다. 내 문제는, 내가 디버그에서 실행할 때 그 connected = false 및 아무도 다른 매개 변수 (예 : timeout) 작동하도록 설정하려고 오전 참조하십시오. 아무도 이것으로 나를 도울 수 있습니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까??android HttpsURLConnection not initializing

누구나 물어보기 전에 내 매니페스트가 인터넷 사용 권한을가집니다.

private static class SyncOperation extends AsyncTask<String, String, Integer> { 

    @Override 
    protected Integer doInBackground(String... params) { 
     HttpsURLConnection urlConnection = null; 
     URL url = null; 
     String msg = "Good"; 
     int code = 0; 
     try { 
      System.setProperty("http.keepAlive", "false"); 
      url = new URL("https://www.google.com"); 
      urlConnection = (HttpsURLConnection) url.openConnection(); 
      urlConnection.setInstanceFollowRedirects(false); 
      urlConnection.setReadTimeout(10000 /* milliseconds */); 
      urlConnection.setConnectTimeout(15000 /* milliseconds */); 
      urlConnection.setRequestMethod("POST"); 
     } catch (MalformedURLException e) { 
      msg = "Bad MalformedURLException"; 
      e.printStackTrace(); 
      Log.e("HTTPS", e.getMessage()); 
     } catch (IOException e) { 
      msg = "Bad IOException"; 
      e.printStackTrace(); 
      Log.e("HTTPS", e.getMessage()); 
     } finally { 
      urlConnection.disconnect(); 
      Log.d("HTTPS", msg); 
      Log.d("HTTPS", "diconnected"); 
     } 

     return code; 
    } 

    @Override 
    protected void onPostExecute(Integer code) { 
     super.onPostExecute(code); 

     Toast t = Toast.makeText(mContext, code.toString(), Toast.LENGTH_SHORT); 
     t.show(); 

    } 

} 

답변

2

연결을 구성하는 중이지만 연결하지 마십시오. url.openConnection() 메서드의 이름은 오해의 소지가 있습니다. 연결을 열지 않습니다. 처음에는 연결되어 있지 않은 연결 개체 만 제공합니다.

연결을 연결하는 메소드를 호출해야합니다. 예 : connect().

즉, 콘텐츠를 읽고 System.out.println()에 글을 쓰는 것과 같이 실제로 연결을 시도하십시오. 그러면 연결이 자동으로 열립니다.

+0

대단히 감사합니다. 오해의 소지가 ... – Meir

관련 문제