使用来自Android的json对象数据和标头将发布请求发送到URL

时间:2017-01-24 12:48:28

标签: android json httpurlconnection

我想使用json对象数据和我的活动标题

向Firebase网址发送请求

我已经检查了来自其他客户端的网址并且我在那里取得了成功,我收到了正确的回复,但是当我从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结构

enter image description here

我没有例外,如果这个请求成功,我应该在我的设备上收到通知,这是从休息客户端而不是来自android,

请检查我在哪里犯了错误,以及如何检查我的请求的回复

1 个答案:

答案 0 :(得分:1)

我解决了我在Logcat中重新编写json的问题,发现它在正确构造后没有正确构建它开始工作,Letme提到我专门用它来通过api进行firebase通知。

我确实喜欢这个:

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