繁体   English   中英

Java,安卓应用程序。 无法发送发布请求 HttpURLConnection conn

[英]Java , android app. Can not send post request HttpURLConnection conn

我正在尝试通过以下步骤发送包含扫描设备的 mac 地址的 post 请求:1/ 扫描设备 2/ 将设备 mac 地址发送到服务器 3/ 以“1”和“-1”返回响应

-----如果 1 -> 设备正确

-----if -1 -> 设备不正确

通过列出设备可以,但是发送 post 请求不起作用,捕获服务器,没有发出请求

private void scanLeDevice(final boolean enable) {
    final Button cancelButton = (Button) findViewById(R.id.btn_cancel);
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                uhf.stopScanBTDevices();
                cancelButton.setText(R.string.scan);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        uhf.startScanBTDevices(new ScanBTCallback() {
            @Override
            public void getDevices(final BluetoothDevice bluetoothDevice, final int rssi, byte[] bytes) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        URL url = null;
                        HttpURLConnection conn = null;
                        String correct_device = "";
                        String device_mac     = bluetoothDevice.getAddress();
                        String device_name    = bluetoothDevice.getName();
                        // response correct_device = 1   == correct device, will available in list
                        // response correct_device = -1  == incorrect, does not need to appear in list
                        
                        try {
                            url = new URL("http://10.10.10.212/api/is_correct_device/");
                            conn = (HttpURLConnection) url.openConnection();
                            conn.setRequestMethod("POST");
                            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                            conn.setRequestProperty("Accept", "application/json");
                            conn.setDoOutput(true);
                            conn.setDoInput(true);

                            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                            os.writeBytes("{ \"check_update\" : " + device_mac + " }");

                            os.flush();
                            os.close();

                            correct_device = conn.getResponseMessage();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        if (correct_device.equals("1")) {
                            MyDevice myDevice = new MyDevice(device_mac, device_name);
                            addDevice(myDevice, rssi);
                        }
                        conn.disconnect();
                    }
                });
            }
        });
        cancelButton.setText(R.string.cancel);
    } else {
        mScanning = false;
        uhf.stopScanBTDevices();
        cancelButton.setText(R.string.scan);
    }
}

谢谢你救我!

您正在从runOnUiThread调用url.openConnection()

这是不可能的!
Android 中的所有网络通信代码都必须在后台线程中运行。

https://developer.android.com/training/basics/network-ops

暂无
暂无

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

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