Android。 C2DM接收身份验证挑战是空异常

问题描述:

我想用谷歌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

使用的URL应该是https://android.clients.google.com/c2dm/send,而不是https://android.apis.google.com/c2dm/send。此外,请确保PARAM_REGISTRATION_ID是一个字符串,等于registration_id,而不是registrationId或类似的东西。