Android 将消息从Google应用程序引擎推送到C2DM服务器

Android 将消息从Google应用程序引擎推送到C2DM服务器,android,google-app-engine,servlets,android-c2dm,Android,Google App Engine,Servlets,Android C2dm,我已经能够 获取服务器授权并将其保存到数据存储; 将手机注册到c2dm服务器,并; 将id发送到将应用程序c2dm注册id保存到数据存储的应用程序服务器。 现在我只想实现一个servlet,它从数据存储中检索服务器令牌号和android应用程序注册id,并使用它们将消息推送到手机 以下是servlet的代码: package com.visd.myfirstapp; import java.io.BufferedReader; import java.io.IOException; impor

我已经能够

获取服务器授权并将其保存到数据存储; 将手机注册到c2dm服务器,并; 将id发送到将应用程序c2dm注册id保存到数据存储的应用程序服务器。 现在我只想实现一个servlet,它从数据存储中检索服务器令牌号和android应用程序注册id,并使用它们将消息推送到手机

以下是servlet的代码:

package com.visd.myfirstapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

//import com.visd.myfirstapp.MessageUtil.CustomizedHostnameVerifier;

public class Visd extends HttpServlet {
    private final static String AUTH = "authentication";

    private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";

    public static final String PARAM_REGISTRATION_ID = "registration_id";

    public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";

    public static final String PARAM_COLLAPSE_KEY = "collapse_key";

    private static final String UTF8 = "UTF-8";
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException 
    {
        resp.setContentType("text/plain");
        Entity appRegIdEntity = null;
        Entity serverTokenEntity = null;
        int RetCode = 0;
        String message = "Congrats C2DM process completed";
        Key appRegIdKEY = KeyFactory.createKey("c2dmreg","cr");
        Key serverTokenKEY = KeyFactory.createKey("vToken", "tokenkn");
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        String appRegId = null, serverToken = null, chk =null;
        try {
             appRegIdEntity = datastore.get(appRegIdKEY);
             serverTokenEntity = datastore.get(serverTokenKEY);
             serverToken = (String) serverTokenEntity.getProperty("token");
             appRegId = (String) appRegIdEntity.getProperty("c2dmid");


             RetCode = sendMessage(serverToken, appRegId, message);

        } catch (EntityNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            chk = "Entered In Exception";       

        }  
        resp.getWriter().println("Return code :" + RetCode + "chk value :" + chk);
    }



    //  Message Sending method

    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);



        URL url = new URL("https://android.clients.google.com/c2dm/send");
        //HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//commented as was causing error, i dont know why
        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;
    }


}
但是浏览器总是显示在异常中输入的RetCode=0和Chk值,即它从不将消息发送到android设备,而是总是在异常中输入。代码中有什么错误我想不出来。。 请帮忙。
谢谢。

这是我最终解决问题的方法,代码帮助:-

public class C2dmsender {
    public static String send(String regid, String appRegId, String mtype, String[] message) throws UnsupportedEncodingException
    {


        String serverToken = ""//give the sever token here;
    data.append("registration_id=" + appRegId);//appRegId is the C2DM id of the device in which you want to push

    // Collapse key is for grouping messages and only the last sent message
    // with the same key going to be sent to the phone when the phone is
    // ready to get the message if its not from the beginning
    data.append("&collapse_key=test");

    // Here is the message we sending, key1 can be changed to what you whant
    // or if you whant to send more then one you can do (i think, not tested
    // yet), Testing is the message here.
    data.append("&data.key1=");

    // If you whant the message to wait to the phone is not idle then set
    // this parameter
    // data.append("&delay_while_idle=1");

    byte[] postData = data.toString().getBytes("UTF-8");


    try {
        // Send data
        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;charset=UTF-8");
        conn.setRequestProperty("Content-Length",
                Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="
                + serverToken);

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

        Integer responseCode = conn.getResponseCode();
        if (responseCode.equals(503)) {
            // the server is temporarily unavailable

        } else {
            if (responseCode.equals(401)) {
                // AUTH_TOKEN used to validate the sender is invalid

            } else {
                if (responseCode.equals(200)) {

                    // Check for updated token header
                    String updatedAuthToken = conn
                            .getHeaderField("Update-Client-Auth");
                    if (updatedAuthToken != null) {
                        serverToken = updatedAuthToken;

                    }

                    String responseLine = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()))
                            .readLine();


            }
        }
    } catch (Exception e) {

        }


    }

}

请编辑您的帖子,并在e.printStackTrace;中包含相应的堆栈跟踪;。