앱을 만들었습니다. 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) {
//....
}
도움 주셔서 감사합니다 :).