简体   繁体   中英

Android java sending a JSON post request, body is empty

I am trying to send a simple request using Java for Android. I have implemented a script and everything seems to be working fine the request is being sent but the most important part the JSON object just doesnt show up on the backend, the body of the request is completely empty.

private BroadcastReceiver onNotice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy);

            String packageName = intent.getStringExtra("package");
            String titleData = intent.getStringExtra("title");
            String textData = intent.getStringExtra("text");
            TableRow tr = new TableRow(getApplicationContext());
            tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            TextView textview = new TextView(getApplicationContext());
            textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            textview.setTextSize(20);
            textview.setTextColor(Color.parseColor("#0B0719"));
            textview.setText(Html.fromHtml(packageName + "<br><b>" + titleData + " : </b>" + textData));
            tr.addView(textview);
            tab.addView(tr);

            try {
                URL url = new URL("https://[hidden].herokuapp.com/api");
                try {

                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("POST");
                    con.setRequestProperty("Content-Type", "application/json; utf-8");
                    con.setRequestProperty("Accept", "application/json");
                    con.setRequestProperty("token", "secret");
                    con.setDoOutput(true);
                    JSONObject jObjectData = new JSONObject();

                    try {
                        jObjectData.put("package", packageName);
                        jObjectData.put("titleData", titleData);
                        jObjectData.put("textData", textData);
                    }catch(JSONException ex) {
                        System.out.println(ex);
                    }

                    System.out.println(jObjectData);
                    byte[] outputInBytes = jObjectData.toString().getBytes("UTF-8");
                    OutputStream os = con.getOutputStream();
                    os.write( outputInBytes );
                    os.close();

                    try (BufferedReader br = new BufferedReader(
                            new InputStreamReader(con.getInputStream(), "utf-8"))) {
                        StringBuilder response = new StringBuilder();
                        String responseLine = null;
                        while ((responseLine = br.readLine()) != null) {
                            response.append(responseLine.trim());
                        }
                        System.out.println(response.toString());
                    }
                }catch (IOException ex){
                    System.out.println(ex);
                }
            } catch (MalformedURLException ex) {
                System.out.println(ex);
            }

        }
    };

I know some things are really messy its because i am very new to java and android, i just need to make this simple script work. The json object is jObjectData which forwards a notification to api, the problem is that whenever i send the request its body wont show up on my Node.js Express backend. The body shows up as {} , i am completely stuck how can i fix this?

For some reason the utf-8 causes this issue. So instead of

con.setRequestProperty("Content-Type", "application/json; utf-8");

I wrote

con.setRequestProperty("Content-Type", "application/json");

and now it works, i have no idea why this is happening, maybe there's some cross configuration about the encoding that caused my server to not recognize the body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM