繁体   English   中英

Android java 发送 JSON 发布请求,正文为空

[英]Android java sending a JSON post request, body is empty

我正在尝试使用 Java 为 Android 发送一个简单的请求。 我已经实现了一个脚本,一切似乎都工作正常,请求正在发送,但最重要的部分 JSON object 只是没有出现在后端,请求的正文完全是空的。

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

        }
    };

我知道有些事情真的很乱,因为我对 java 和 android 很陌生,我只需要让这个简单的脚本工作。 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. 身体显示为{} ,我完全卡住了我该如何解决这个问题?

由于某种原因, utf-8会导致此问题。 所以而不是

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

我写

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

现在它起作用了,我不知道为什么会这样,也许有一些关于编码的交叉配置导致我的服务器无法识别身体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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