使用 json object 数据和来自 Android 的 Headers 向 URL 发送 Post 请求

Sending Post Request to URL with json object Data and Headers from Android

我想用 json object 数据和来自我的 activity ,

的 headers 向 Firebase url 发送请求

我已经从其他客户端检查了 url,我在那里成功了,我收到了正确的响应,但是当我从 android 调用它时,我无法得到响应:

这是我的代码:

 public static void pushFCMNotification(String userDeviceIdKey) throws     Exception{

        String authKey = AUTH_KEY_FCM;   // You FCM AUTH key
        String FMCurl = API_URL_FCM;

        URL url = new URL(FMCurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","application/json");
        conn.setRequestProperty("Authorization","key="+authKey);
        Log.e("authkey==> ",authKey+"");

        JSONObject json = new JSONObject();
        json.put("to",userDeviceIdKey.trim());
        Log.e("deviceidkey==> ",userDeviceIdKey.trim()+"");

        JSONObject info = new JSONObject();
        info.put("title", "Notificatoin Title");   // Notification title
        info.put("body", "Hello Test notification"); // Notification body
        json.put("notification", info);

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();
        conn.getInputStream();
    }

这是我要用 url

发送的 json 结构

我没有例外,如果此请求成功,我应该会在我的设备上收到一条通知,该通知来自其他客户端,而不是来自 android,

请检查我在哪里犯了错误以及如何检查我的请求的响应

我解决了我在 Logcat 中调试 json 的问题,amd 发现它在使其正确后开始工作后结构不正确,让我提一下我专门通过 api.

我喜欢这个:

public static String makeRequest(String id) throws JSONException {
        HttpURLConnection urlConnection;
        JSONObject json = new JSONObject();
        JSONObject info = new JSONObject();
        info.put("title", "Notification Title");   // Notification title
        info.put("body", "Notification body"); // Notification body
        info.put("sound", "mySound"); // Notification sound
        json.put("notification", info);
        json.put("to","INSTANCE ID FETCHED FOR SIGNLE DEVICE HERE");
        Log.e("deviceidkey==> ",id+"");
        Log.e("jsonn==> ",json.toString());
        String data = json.toString();
        String result = null;
        try {
            //Connect
            urlConnection = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Authorization", "key=YOUR FIREBASE SERVER KEY");
            urlConnection.setRequestMethod("POST");
            urlConnection.connect();

            //Write
            OutputStream outputStream = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            writer.write(data);
            writer.close();
            outputStream.close();

            //Read
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

            String line = null;
            StringBuilder sb = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }

            bufferedReader.close();
            result = sb.toString();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }