Android系统。 C2DM接收的身份验证挑战是空的例外

时间:2012-02-06 09:26:24

标签: android authentication android-c2dm

我正在尝试使用谷歌c2dm在Android设备上发送推送通知,但我有一点问题。 当我尝试发送消息时,我得到此异常“java.IOException Received Authentication Challenge is Null”我不知道我做错了什么。我正在使用Vogella tutorial发送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代码,但我无法理解其原因,auth令牌很好。

1 个答案:

答案 0 :(得分:0)

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