Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:01:36+00:00 2026-05-26T10:01:36+00:00

I receive messages only on one phone. One normal message with my informaion and

  • 0

I receive messages only on one phone. One normal message with my informaion and one empty message which crash my application.
I change the registration id key and the autorization key.
When it’s only one phone work perfect.

public class URLCaller {

private static final String PASSWORD = "xxxxxxx"; //i remove it

private static final String EMAIL = "xxxxxxxxxx@gmail.com"; //i remove it

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

static String auth_key = null;

static byte[] postData = null;

public static void callGoogle(CalEvent calEvent) throws IOException, SystemException {

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

    StringBuilder builder = new StringBuilder();

    HttpsURLConnection conn = null;
    // For each smartPhone
    for(SmartPhone smartPhone : SmartPhoneLocalServiceUtil.getSmartPhones(0, SmartPhoneLocalServiceUtil.getSmartPhonesCount())) {
        auth_key = getAuthorization();
        //Setup data
        System.out.println("" + smartPhone.getRegistrationId());
        builder.append("registration_id=" + smartPhone.getRegistrationId());

        builder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
        builder.append("&").append("data.payload").append("=").append(URLEncoder.encode("calEventId=" + calEvent.getCalEventId(), "UTF-8"));
        postData = builder.toString().getBytes("UTF-8");

        //Calling server
        conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(new CustomizedHostnameVerifier());
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        conn.setRequestProperty("Content_Lenght", Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_key);

        // Issue the HTTP POST request
        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();

        System.out.println("Google server said: " + conn.getResponseCode() + ", " + conn.getResponseMessage());

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

            System.out.println("C2DM," + "Got updated auth token from datamessaging servers: " + updatedAuthToken);

            //auth_key = updatedAuthToken;

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

            // NOTE: You *MUST* use exponential backoff if you receive a 503 response code.
            // Since App Engine's task queue mechanism automatically does this for tasks that
            // return non-success error codes, this is not explicitly implemented here.
            // If we weren't using App Engine, we'd need to manually implement this.
            if(responseLine == null || responseLine.equals("")) {
                System.out.println("Got " + conn.getResponseCode() + " response from Google AC2DM endpoint.");
                throw new IOException("Got empty response from Google AC2DM endpoint.");
            }

            String[] responseParts = responseLine.split("=", 2);
            if(responseParts.length != 2) {
                System.out.println("Invalid message from google: " + conn.getResponseCode() + " " + responseLine);
                throw new IOException("Invalid response from Google " + conn.getResponseCode() + " " + responseLine);
            }

            if(responseParts[0].equals("id")) {
                System.out.println("Successfully sent data message to device: " + responseLine);
            }

            if(responseParts[0].equals("Error")) {
                String err = responseParts[1];
                System.out.println("Got error response from Google datamessaging endpoint: " + err);
                // No retry.
                //throw new IOException(err);
            }
        }

    }

}

public static String getAuthorization() {
    String authToken = null;

    HttpURLConnection urlConnection;
    URL url;
    try {
        url = new URL("https://www.google.com/accounts/ClientLogin");

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        StringBuilder builder = new StringBuilder();
        builder.append("Email=").append(EMAIL);
        builder.append("&Passwd=").append(PASSWORD);
        builder.append("&accountType=GOOGLE");
        builder.append("&source=Google-C2DM-Example");
        builder.append("&service=ac2dm");

        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(builder.toString().getBytes("UTF-8"));
        outputStream.close();
        int responseCode = urlConnection.getResponseCode();
        System.out.println(responseCode + "\tSuccess");
        StringBuffer resp = new StringBuffer();
        if(responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = rd.readLine()) != null) {
                if(line.startsWith("Auth=")) {
                    resp.append(line.substring(5));
                }
            }
            rd.close();
        }

        authToken = resp.toString();
        System.out.println("AuthTOken" + authToken);
    } catch(MalformedURLException e) {
        e.printStackTrace();
    } catch(ProtocolException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }
    return authToken;

}

private static class CustomizedHostnameVerifier implements HostnameVerifier {

    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

I do not get any errors.
Thank You, good day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:01:36+00:00Added an answer on May 26, 2026 at 10:01 am

    StringBuilder builder = new StringBuilder(); was before my for 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using thread which will receive messages from the external application.So my thread
I'm implementing a little chat application where I receive messages from a server, which
I am developing an application in which I continuously receive messages. I am storing
I'm creating a console application that should receive messages from the network in order
In our application, we receive messages from JMS Topic. First I want to know
I have requirement in which I continuously receive messages that needs to be written
I have an application which uses SslStream to send and receive data with its
I have an SslStream from which I receive spontaneous XML messages. I want to
Problem is like this: I have two winapi application's. There is only one way
I receive messages from a Google Talk account, they are shown in the Table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.