Trying to HTTP-POST to GCM from Java

2019-09-05 21:49发布

问题:

I'm trying to send a HTTP-POST to the Google Cloud Messaging service. I have setup the correct keys, and everything is working when I use a php script for sending push notifications to my cellphone.

But my Java httpPost only returns a 401 response. I have followed the instructions given at Android Developers but I'm still getting the annoying 401. Am I assigning the header fields wrong?

My Code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

    public class Server {
      public static void main(String[] args) throws IOException {

          String url = "https://android.googleapis.com/gcm/send";

            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(url);


            HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");

            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
            urlParameters.add(new BasicNameValuePair("registration_id=", "MY_DEVICE_GSM_REG_ID"));
            urlParameters.add(new BasicNameValuePair("data=", "Type=value,Lat=58.365547,Long=8.613235,Comment=value"));

            httppost.setHeader("Authorization",
                    "key=MY_API_AUTH_FROM_GOOGLE_API_CONSOLE_BROWSER_TOKEN");
            httppost.setHeader("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");

            post.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));

            HttpResponse response = client.execute(post);
            System.out.println("Response Code : " 
                        + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
}
}

回答1:

You are setting the Authorization header correctly. There is probably a problem with your API Key.

You do have a problem in your registration ID and payload (which is not related to the 401 error).

This is wrong :

urlParameters.add(new BasicNameValuePair("registration_id=", "MY_DEVICE_GSM_REG_ID"));
urlParameters.add(new BasicNameValuePair("data=", "Type=value,Lat=58.365547,Long=8.613235,Comment=value"));

You should remove the = from the key and each payload parameter should start with data.. Therefore you should have:

urlParameters.add(new BasicNameValuePair("registration_id", "MY_DEVICE_GSM_REG_ID"));
urlParameters.add(new BasicNameValuePair("data.Type", "value"));
urlParameters.add(new BasicNameValuePair("data.Lat", "58.365547"));
urlParameters.add(new BasicNameValuePair("data.Long", "8.613235"));
urlParameters.add(new BasicNameValuePair("data.Comment", "value"));