【问题标题】:Sending Post Request to URL with json object Data and Headers from Android使用来自 Android 的 json 对象数据和标头向 URL 发送 Post 请求
【发布时间】:2017-01-24 12:48:28
【问题描述】:

我想使用我的活动中的 json 对象数据和标头向 Firebase url 发送请求,

我已经检查了来自 rest 客户端的 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 结构

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

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

【问题讨论】:

    标签: android json httpurlconnection


    【解决方案1】:

    我解决了我在 Logcat 中调试 json 的问题,发现它在正确后开始工作后结构不正确,让我提一下,我专门使用它通过 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多