Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向谷歌云消息发送改装后的帖子_Java_Post_Server_Google Cloud Messaging_Retrofit - Fatal编程技术网

Java 向谷歌云消息发送改装后的帖子

Java 向谷歌云消息发送改装后的帖子,java,post,server,google-cloud-messaging,retrofit,Java,Post,Server,Google Cloud Messaging,Retrofit,gcmr响应: Main: 当我通过改造向GCM发送帖子时,它总是返回回复“400 InvalidTokenFormat”,互联网上没有文档 我在代码中做错了什么?这个响应意味着什么?您的服务实际是如何实现的?我看不出您是如何为注册\u id字段输入键名的。@ztan我编辑了问题,添加了使用“注册\u id”的主方法,首先在debug in response对象中检查您的请求url-看起来应该是基本url,/gcm/send-应该是POST批注中的字符串。但这不是你问题的根源。最可能的情况是

gcmr响应:

Main:

当我通过改造向GCM发送帖子时,它总是返回回复“400 InvalidTokenFormat”,互联网上没有文档


我在代码中做错了什么?这个响应意味着什么?

您的
服务实际是如何实现的?我看不出您是如何为
注册\u id
字段输入键名的。@ztan我编辑了问题,添加了使用“注册\u id”的主方法,首先在debug in response对象中检查您的请求url-看起来应该是基本url,/gcm/send-应该是POST批注中的字符串。但这不是你问题的根源。最可能的情况是json格式错误(HTML400就是这么解释的),所以最好用嵌套对象重写GCMRequest
public class GCMResponse {

    public String multicast_id;
    public int success;
    public int failure;
    public int canonical_ids;
    public List<GCMObject2> results;

    public class GCMObject2 {
        public int message_id;
        public int registration_id;
        public String error;
    }
}
public class GCMRequest {
    public List<String> registration_ids;
    public Map<String, String> data;

    public GCMRequest(String title, String message, String icon) {
        registration_ids = new LinkedList<>();

        data = new HashMap<>();

        data.put("title", title);
        data.put("message", message);
        data.put("icon", icon);
    }

    public void addRegId(String regId) {
        registration_ids.add(regId);
    }
}
public interface RetrofitService {
    @Headers("Content-Type: application/json")
    @POST("/")
    public GCMResponse GCMAuthorization(@Header("Authorization") String apiKey, @Body GCMRequest data);
}
private static final String GCM_SERVER_URL = "https://android.googleapis.com/gcm/send";

private static void sendWithRetrofit(String apiKey, GCMRequest gcmRequest) {
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(GCM_SERVER_URL)
            .build();

    final RetrofitService service = restAdapter.create(RetrofitService.class);

    try {
        GCMResponse gcmResponse = service.GCMAuthorization("key=" + apiKey, gcmRequest);
        System.out.println(gcmResponse);
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
}

public static void main(String[] args) {
    GCMRequest gcmRequest = new GCMRequest("Title", "message", "" + R.drawable.icon1);
    gcmRequest.addRegId("APA91bGL1GJ9eYGEIkGgcCXTOnMd_WPbacJH9UhztOWplcGcRXXXXXXXXXXXXXXXXXXXXXXXX");

    GCMPoster.post(API_KEY, gcmRequest);
}