2012-02-06 74 views
0

我想用谷歌c2dm在Android設備上發送推送通知,但我有一個小問題。 當我試圖發送消息時,我得到這個異常「java.IOException接收到的認證挑戰是空的」我不知道我在做什麼錯。我正在使用Vogella tutorial發送c2dm。Android。 C2DM接收身份驗證挑戰是空異常

public static String getToken(String email, String password) 
     throws IOException { 
    // Create the post data 
    // Requires a field with the email and the password 
    StringBuilder builder = new StringBuilder(); 
    builder.append("Email=").append(email); 
    builder.append("&Passwd=").append(password); 
    builder.append("&accountType=GOOGLE"); 
    builder.append("&source=MyLittleExample"); 
    builder.append("&service=ac2dm"); 

    // Setup the Http Post 
    byte[] data = builder.toString().getBytes(); 
    URL url = new URL("https://www.google.com/accounts/ClientLogin"); 
    HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
    con.setUseCaches(false); 
    con.setDoOutput(true); 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 
    con.setRequestProperty("Content-Length", Integer.toString(data.length)); 

    // Issue the HTTP POST request 
    OutputStream output = con.getOutputStream(); 
    output.write(data); 
    output.close(); 

    // Read the response 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      con.getInputStream())); 
    String line = null; 
    String auth_key = null; 
    while ((line = reader.readLine()) != null) { 
     if (line.startsWith("Auth=")) { 
      auth_key = line.substring(5); 
     } 
    } 

    // Finally get the authentication token 
    // To something useful with it 
    return auth_key; 
} 

身份驗證令牌很好,並返回200代碼。但是發送消息是失敗

public static int sendMessage(String auth_token, String registrationId, 
     String message) throws IOException { 

    StringBuilder postDataBuilder = new StringBuilder(); 
    postDataBuilder.append(PARAM_REGISTRATION_ID).append("=") 
      .append(registrationId); 
    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); 

    // Hit the dm URL. 

    URL url = new URL("https://android.apis.google.com/c2dm/send"); 
    HttpsURLConnection 
      .setDefaultHostnameVerifier(new CustomizedHostnameVerifier()); 
    HttpsURLConnection conn = (HttpsURLConnection) 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_token); 

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

    int responseCode = conn.getResponseCode(); 
    return responseCode; 
} 

當我從其他的帖子理解這個錯誤是指401碼,但我不明白這個原因,身份驗證令牌是好的。

+0

我只是試圖用curl命令發送消息並獲得了200個代碼,但推送消息未到達我的設備。我不知道爲什麼。之後,我嘗試再次從我的代碼發送推送,並獲得代碼200。 – 2012-02-06 11:10:13

回答

0

使用的URL應該是https://android.clients.google.com/c2dm/send,而不是https://android.apis.google.com/c2dm/send。此外,請確保PARAM_REGISTRATION_ID是一個字符串,等於registration_id,而不是registrationId或類似的東西。

相關問題