C2DM,IOException异常时发送消息 [英] C2DM, IOException when sending message

查看:131
本文介绍了C2DM,IOException异常时发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让C2DM的发送部分工作。

I can't get the sending part of C2DM to work. I get IOException at OutputStream out = conn.getOutputStream();

public void sendMessage() throws IOException{       
                String key = readFileAsString("acct/"+to+"/key");

                StringBuilder postDataBuilder = new StringBuilder();
                postDataBuilder.append("registration_id").append("=").append(key);
                postDataBuilder.append("&").append("collapse_key").append("=").append("0");
                postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(to+"--"+"acct/"+to+"/1", UTF8));
                byte[] postData = postDataBuilder.toString().getBytes(UTF8);

                URL url = new URL("https://android.apis.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");
                conn.setRequestProperty("Content-Length", Integer.toString(postData.length));                   
                conn.setRequestProperty("Authorization", "GoogleLogin auth=" + cl);

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

                int sw =conn.getResponseCode();
                System.out.println(""+sw);
                switch (sw) {
                    case 200:
                        System.out.println("Success, but check for errors in the body");
                        break;
                    case 503:
                        System.out.println("Service unavailable");
                        break;
                    case 401:
                        System.out.println(" Invalid authentication token");
                        break;
                 }

            }

感谢您!

推荐答案

解决方案最常用给予解决您收到错误消息来定义自定义主机名验证。你面临的主要问题是,谷歌的Andr​​oid URL返回的域名是* .google.com。不幸的是,这会导致一些问题,因为Android的SDK是android.apis.google.com。该JVM将不会验证默认情况下这个组合(* .sdk.google.com是可以接受的)。

The solution most commonly given to resolve the error message you are receiving is to define a custom Hostname validation. The main problem that you are facing is that the domain name returned by Google's Android URL is *.google.com. Unfortunately, this causes some issues as the Android SDK is at android.apis.google.com. The JVM will not validate this combination by default (*.sdk.google.com would be acceptable).

下面是如何创建自己的主机名验证器的例子:

Here is an example of how you can create your own hostname validator:

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

    HostnameVerifier hVerifier = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession
                session) {
            return true;
        }
    };

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setHostnameVerifier(hVerifier);

如果你改变code到类似上述的东西,你提到的错误应该纠正。

If you change your code to something similar to above, the error you mentioned should be corrected.

修改结果
下面是一些测试code,它是在我的开发环境功能齐全。刚刚更新的验证信息与应用程序的特定信息来运行:

EDIT
Here is some test code that is fully functional in my dev environment. Just updated the authentication information with your application specific information to run:

public class C2DMTest {


    public static void main(String... args) throws Exception {
        String auth = authorize();
        if (auth == null) {
            System.out.println("No authorization returned");
            System.exit(1);
        }
        sendMessage(auth);
    }

    /**
     * Perform an authorization request to access Google's C2DM
     * API.
     *
     * @return The retrieved authorization request.
     */
    private static String authorize() throws Exception {
        String accountType = "GOOGLE";
        String service = "ac2dm";

        String source = "replace-me";
        String email = "replace-me";
        String passwd = "replace-me";


        StringBuilder params = new StringBuilder();
        params.append("accountType=").append(accountType)
                .append("&Email=").append(URLEncoder.encode(email, UTF8))
                .append("&Passwd=").append(URLEncoder.encode(passwd, UTF8))
                .append("&service=").append(service)
                .append("&source=").append(source);
        byte[] postData = params.toString().getBytes(UTF8);

        URL url = new URL("https://www.google.com/accounts/ClientLogin");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(postData.length));

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

        int sw = conn.getResponseCode();
        System.out.println("" + sw);

        switch (sw) {
            case 503:
                System.out.println("Service unavailable");
                break;
            case 401:
                System.out.println(" Invalid authentication token");
                break;
            default:
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream in = conn.getInputStream();
                byte[] bytes = new byte[100];
                int len = -1;
                while ((len = in.read(bytes)) != -1) {
                    baos.write(bytes, 0, len);
                }
                in.close();
                String input = baos.toString();
                Map<String, String> res = parseResponse(input);
                return res.get("Auth");
        }
        return null;
    }

    /**
     * Parses a response string into a usable data map.
     *
     * @param response The response from Google
     * @return A Map representation.
     */
    private static Map<String, String> parseResponse(String response) {
        Map<String, String> map = new HashMap<String, String>();
        if (response != null) {
            String[] lines = response.split("\n");
            for (String line : lines) {
                String[] parts = line.split("=");
                if (parts.length == 2) {
                    map.put(parts[0], parts[1]);
                }
            }
        }
        return map;
    }

    private static String UTF8 = "UTF-8";


    /**
     * Send message to mobile device.
     *
     * @param cl Google API auth code.
     */
    public static void sendMessage(String cl) throws IOException {
        String key = "invalid";

        StringBuilder postDataBuilder = new StringBuilder();
        postDataBuilder.append("registration_id").append("=").append(key);
        postDataBuilder.append("&").append("collapse_key").append("=").append("0");
        postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode("test-content", UTF8));
        byte[] postData = postDataBuilder.toString().getBytes(UTF8);

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

        HostnameVerifier hVerifier = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession
                    session) {
                return true;
            }
        };

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(hVerifier);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="+cl);

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

        int sw = conn.getResponseCode();
        System.out.println("" + sw);
        switch (sw) {
            case 200:
                System.out.println("Success, but check for errors in the body");
                break;
            case 503:
                System.out.println("Service unavailable");
                break;
            case 401:
                System.out.println(" Invalid authentication token");
                break;
        }

    }
}

这篇关于C2DM,IOException异常时发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆