当前位置: 首页 > 知识库问答 >
问题:

向FCM服务器发布JSON请求不工作

缪茂勋
2023-03-14
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
package com.example.artin.pushnotifications;

import android.os.AsyncTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

class AsyncT extends AsyncTask<Void,Void,Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            URL url = new URL("https://fcm.googleapis.com/fcm/send"); //Enter URL here
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
            httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
            httpURLConnection.setRequestProperty("Authorization","key=AIzaSyDZx9l_Izta9AjVS0CX70ou8OjbDVVGlHo");
            httpURLConnection.connect();

            JSONObject jsonObject = new JSONObject();
            JSONObject param = new JSONObject();
            param.put("Hii","there");

            jsonObject.put("data",param);
            jsonObject.put("to", "dXazhmeFSSU:APA91bG23o75zeNOCb7pY-OCQG4BsGbY-YZrSnDrvLWv1");

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


}

当按钮被按下时,我执行它

AsyncT asyncT = new AsyncT();
asyncT.execute();

共有1个答案

郑锋
2023-03-14

我使用了OkHttp,现在它起作用了。如果有人需要,这里是代码。

首先将OkHttp添加到apps graddle.build中

compile 'com.squareup.okhttp3:okhttp:3.4.1'

下面是发送POST Json请求的方法

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient();

    Call post(String url, String json, Callback callback) {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .addHeader("Content-Type","application/json")
                .addHeader("Authorization","key=YourApiKey")
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
        return call;
    }
try {
    JSONObject jsonObject = new JSONObject();
    JSONObject param = new JSONObject();
    param.put("Hii", "there");
    param.put("Hours", "12:50");
    jsonObject.put("data", param);
    jsonObject.put("to", "TokenOfTheDevice");
    post("https://fcm.googleapis.com/fcm/send", jsonObject.toString(), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //Something went wrong
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String responseStr = response.body().string();
                        Log.d("Response", responseStr);
                        // Do what you want to do with the response.
                    } else {
                        // Request not successful
                    }
                }
            }
    );
} catch (JSONException ex) {
    Log.d("Exception", "JSON exception", ex);
}
 类似资料:
  • 使用Ionic 2和angular http我试图向https://fcm.googleapis.com/fcm/send发出http post请求,用postman测试的请求工作得非常好 这个问题是我这个问题的后续问题: http请求的代码是 来自chrome控制台的标题如下: 一般标题 请求URL:https://fcm.googleapis.com/fcm/send请求方法:POST状态代码

  • 我正在使用带有HTTP本机模块的Ionic 2向FCM服务器发出推送通知的发布请求。我使用的代码是: 它给我一个错误: 我用Postman尝试了发布请求,它可以完美地传递推送通知。 Postman的代码是: 问题: > 如果我试图添加一个来获取服务器的响应,它会给我一个错误https://github.com/wymsee/cordova-HTTP

  • XMLHttpRequest 对象用于和服务器交换数据。 向服务器发送请求 如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); 方法 描述 open(method,url,async) 规定请求的类型、URL 以及是否异步处理

  • 我试图从java向一个现有的RESTful服务发出一个< code>POST请求,以便获得json响应并将其存储在一个字符串中。 让我们假设RESTful Service Url如下所示: 这是一种带有产品的商店,您想要搜索商品。为了进行搜索,您还必须发送, 如下所示: 为了发布这篇文章,我在java中实现了以下方法: 例外情况如下: 我不知道为什么我会得到这个例外,但如果你有一个想法或者你也面临

  • 问题内容: 当我在Node服务器上打印请求的内容时,在任何地方都看不到用户数据。 这是我的节点服务器: 这是Angular2代码: 任何人都可以帮我或解释如何处理角度的http请求。 问题答案: 那是你的服务器: 那是您的有角度的客户: 回购https://github.com/kuncevic/angular-httpclient- examples

  • 我正在使用Android截击来发出请求。所以我使用这个代码。我不明白一件事。我在服务器中检查params是否始终为null。我认为GETPARAMS()不工作。我应该做些什么来解决这个问题。