2011-08-01 5 views
0

앱을 만들었습니다. Google의 C2DM 서비스를 사용하는 Android 용 나는 일부 튜토리얼에서 서버 시뮬레이터를 만들었고 정상적으로 작동한다. 내 문제는 자바 서블릿을 만들려고했습니다. Android 기기에서 메시지를 수신하고 등록 ID를 저장하지만 Google C2DM 서버에 https POST 요청을 보내려고하면 항상 은 SocketTimeoutException을 가져옵니다. 가져 오는 중에 시간 초과 : https://android.clients.google.com/c2dm/send. 동일한 이유가 Android 기기에서 작동하는 이유를 알 수 없습니다. 코드는 다음과 같습니다.C2DM에서 HTTP Post가 SocketTimeoutException을 제공합니다.

//The AuthToken from Google Client Login 

String auth_key = TOKEN;  
StringBuilder postDataBuilder = new StringBuilder(); 

//some parameters to pass, I've checked and it's correct, it's working 
//with Fiddler 
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(REGISTRATION_ID); 
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); 
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8)); 

byte[] postData = postDataBuilder.toString().getBytes(UTF8); 
URL url = new URL("https://android.clients.google.com/c2dm/send"); 

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setDoOutput(true); 
conn.setUseCaches(false); 

conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 

conn.setRequestProperty("Content-Length",Integer.toString(postData.length)); 
conn.setRequestProperty("Authorization", "GoogleLogin auth="+auth_key); 

OutputStream out = conn.getOutputStream(); 
out.write(postData); 
out.close(); 

int responseCode = conn.getResponseCode(); 
//here comes the error processing, but I can't reach it, because of 
//the exception. 



if (responseCode == 401 || responseCode == 403) { 

//.... 
} 

도움 주셔서 감사합니다 :).

답변

0

확인할 첫 번째 확실한 점은 - 사과하신 점이 있다면 - 프록시 서버를 사용하고 계신지 확인하는 것입니다. 회사 방화벽? 그렇다면 타임 아웃은 정확히 위의 코드에서 기대할 수있는 증상입니다. (이것은 항상 나를 잡아 먹는다!)

(HttpURLConnection 선언에있는) 코드의 후반 부분을 수정하지 않으면 시간 초과가 발생한다. 다음과 같이 HttpURLConnection의 공장에 전달 프록시 개체의

  • 또한 :

    프록시 프록시 = 새로운 프록시 ((회사 방화벽) 내 시스템에 두 개의 변화에 ​​나는 다시 200 OK를 얻을 수 Proxy.Type.HTTP, 새로운 InetSocketAddress ("...", 8080)); HttpURLConnection conn = (HttpURLConnection) url.openConnection (proxy);

  • 내 JVM에서 신뢰할 수없는 C2DM 서버의 인증서를 수락합니다. 테스트 목적으로 Trusting all certificates using HttpClient over HTTPS에서 설명한대로 기본 호스트 이름 확인 프로그램과 TrustManager를 무시합니다. 프로덕션을 위해보다 안전한 솔루션을 찾아야합니다.

내가 발견 한 또 다른 것; 문제가되지 않는 것 같지만 http://code.google.com/android/c2dm/index.html#push은 android.clients.google.com이 아닌 https://android.apis.google.com/c2dm/send에 글을 남기고 있습니다. 미래에 깨질 수 있다는 사실을 알고 있어야합니다. 대신

URL url = new URL("http://android.apis.google.com/c2dm/send"); 

:

URL url = new URL("https://android.apis.google.com/c2dm/send"); 

이 나를 위해 일

0

나는 같은 문제와 내가 시도했다

에 직면했다.

관련 문제