Adnan Quraishi
Adnan Quraishi

Reputation: 533

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

I want to send request to Firebase url with json object data and headers from my activity ,

I have checked the url from rest client and i am at success there , i am receiving correct response , but when i call this from android i am unable to get response:

here is my code:

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

this is the json structure i want to send with url

enter image description here

i got no exception and on success of this request i should get a notification on my device , which is hapening from rest client but not from android ,

kindly check where i am making a mistake and how can check response of my request

Upvotes: 0

Views: 3851

Answers (1)

Adnan Quraishi
Adnan Quraishi

Reputation: 533

I solved the problem i debbuged the json in Logcat amd found it was not correctly structured after making it correct it starts working, Letme Mention i am specifically using this for firebase notification through api .

i did like this :

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

Upvotes: 1

Related Questions