2011-08-01 38 views
0

我做了一個應用程序。適用於使用Google的C2DM服務的Android。 I 從一些教程做了一個服務器模擬器,它工作正常。我的 的問題是,我試圖建立一個Java Servlet。從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聲明),未修改,我看到一個超時;我的系統(後面的公司防火牆)上,有兩個變化,我得到一個200 OK回:

  • 除了傳遞給HttpURLConnection的工廠代理對象如下:

    代理代理=新代理( Proxy.Type.HTTP,新的InetSocketAddress(「...」,8080)); HttpURLConnection conn =(HttpURLConnection)url.openConnection(proxy);

  • 接受我的JVM不信任的C2DM服務器的證書。出於測試目的,我將覆蓋默認主機名驗證者和TrustManager,如Trusting all certificates using HttpClient over HTTPS中所述。對於生產你應該看看更安全的解決方案。

我發現的另一件事;這似乎並不重要,但http://code.google.com/android/c2dm/index.html#push說要張貼到https://android.apis.google.com/c2dm/send,而不是android.clients.google.com - 只是要注意,可能會在未來打破。

0

我面臨同樣的問題,

我曾嘗試:

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

代替:

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

它爲我工作。