试图HTTP-POST从Java GCM [英] Trying to HTTP-POST to GCM from Java

查看:172
本文介绍了试图HTTP-POST从Java GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个HTTP-POST发送到谷歌云消息传递服务。我已经安装了正确的密钥,一切工作正常,当我使用PHP脚本发送推送通知到我的手机。

但我的Java httpPost只返回一个401响应。我已按照 Android开发者的指示,但我仍然得到恼人的401.我是否分配报头字段错了吗?

我的code:

 进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.List;进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.HttpClientBuilder;
进口org.apache.http.message.BasicNameValuePair;    公共类服务器{
      公共静态无效的主要(字串[] args)抛出IOException          字符串URL =htt​​ps://android.googleapis.com/gcm/send;            HttpClient的客户端= HttpClientBuilder.create()建立()。
            HttpPost后=新HttpPost(URL);
            HttpPost httppost =新HttpPost(https://android.googleapis.com/gcm/send);            清单<&的NameValuePair GT; urlParameters =新的ArrayList<&的NameValuePair GT;();
            urlParameters.add(新BasicNameValuePair(registration_id =,MY_DEVICE_GSM_REG_ID));
            urlParameters.add(新BasicNameValuePair(数据=,类型=值,纬度= 58.365547,龙= 8.613235,注释=价值));            httppost.setHeader(授权,
                    键= MY_API_AUTH_FROM_GOOGLE_API_CONSOLE_BROWSER_TOKEN);
            httppost.setHeader(内容类型,
                    应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8);            post.setEntity(新UrlEn codedFormEntity(urlParameters,UTF-8));            HTT presponse响应= client.execute(岗位);
            的System.out.println(响应code:
                        + response.getStatusLine()的getStatus code());            RD的BufferedReader =新的BufferedReader(
                    新的InputStreamReader(response.getEntity()的getContent()));            StringBuffer的结果=新的StringBuffer();
            串线=;
            而((行= rd.readLine())!= NULL){
                result.append(线);
            }
}
}


解决方案

您是否正确设置Authorization头。有可能是你的API密钥的问题。

您做您的注册ID和有效载荷(这是不相关的401错误)有问题。

这是错误的:

  urlParameters.add(新BasicNameValuePair(registration_id =,MY_DEVICE_GSM_REG_ID));
urlParameters.add(新BasicNameValuePair(数据=,类型=值,纬度= 58.365547,龙= 8.613235,注释=价值));

您应该从键删除 = ,每个有效载荷参数应与数据开始。。因此,你应该有:

  urlParameters.add(新BasicNameValuePair(registration_id,MY_DEVICE_GSM_REG_ID));
urlParameters.add(新BasicNameValuePair(data.Type,值));
urlParameters.add(新BasicNameValuePair(data.Lat,58.365547));
urlParameters.add(新BasicNameValuePair(data.Long,8.613235));
urlParameters.add(新BasicNameValuePair(data.Comment,值));

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

解决方案

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

这篇关于试图HTTP-POST从Java GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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