通过Android应用程序向Apache服务器发送请求,摆脱无限循环

问题描述 投票:1回答:1

这是我的项目的架构:

1)Android应用程序 - > 2)RPi上的Apache服务器 - > 3)控制 - > 4)我的设备的Python脚本

1)Android应用程序 - 2个简单的开关,每个都在RPi上向Apache发送不同的请求:

public class MainActivity extends Activity{

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Switch switch_auto = findViewById(R.id.switch_auto);
    Switch switch_manual = findViewById(R.id.switch_manual);

    switch_auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                new Background_get().execute("led1=1");
            } else {
                new Background_get().execute("led1=0");
            }
        }
    });

    switch_manual.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Log.d(TAG, "onCheckedChanged: SET TO TRUE");
                new Background_get().execute("led2=1");
                Log.d(TAG, "onCheckedChanged: EXIT");
            } else {
                Log.d(TAG, "onCheckedChanged: SET TO FALSE");
                new Background_get().execute("led2=0");
                Log.d(TAG, "onCheckedChanged: EXIT");
            }
        }
    });

}

private class Background_get extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL("http://192.168.0.248/?" + params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder result = new StringBuilder();
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine).append("\n");

            in.close();
            connection.disconnect();
            return result.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
}

2)RPi上的Apache服务器 - 非常简单的PHP代码:

  • 如果它是“led1 = 1”,启动Python脚本,使设备工作10秒(time.sleep(10)
  • 如果它是“led1 = 0”,只需打印一些东西
  • 如果它是“led2 = 1”,则启动另一个Python脚本,使设备无限期工作(while True: time.sleep(1)
  • 如果它是“led2 = 0”,则启动第三个停止设备的Python脚本

我的问题:我可以通过网络浏览器使用这些脚本正确控制我的设备,例如:

出于测试目的,这是针对led1的Apache2的access.log(= 1使其工作10秒,= 0只是打印一行):

192.168.0.10 - - [01 / Mar / 2018:10:42:01 +0100]“GET /?led1 = 1 HTTP / 1.1”200 411“ - ”“Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit /537.36(KHTML,与Gecko一样)Chrome / 64.0.3282.186 Safari / 537.36“

(它开始工作)

192.168.0.10 - - [01 / Mar / 2018:10:42:05 +0100]“GET /?led1 = 0 HTTP / 1.1”200 233“ - ”“Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit /537.36(KHTML,与Gecko一样)Chrome / 64.0.3282.186 Safari / 537.36“

(没有问题,在4秒后得到led1 = 0并且led1 = 1的脚本仍然可以工作几秒钟)

不幸的是,我的Android应用程序无法做到这一点。这是Apache的日志:

192.168.0.66 - - [01 / Mar / 2018:10:3​​6:53 +0100]“GET /?led1 = 1 HTTP / 1.1”200 411“ - ”“Dalvik / 2.1.0(Linux; U; Android 8.1。 0; Nexus 5X Build / OPM3.171019.014)“

(我在5秒后做出“关闭”但......)

192.168.0.66 - [01 / Mar / 2018:10:3​​7:07 +0100]“GET /?led1 = 0 HTTP / 1.1”200 232“ - ”“Dalvik / 2.1.0(Linux; U; Android 8.1。 0; Nexus 5X Build / OPM3.171019.014)“

(它没有!注意差异14秒(它发射约3秒然后工作10秒)。它在led1 = 1的脚本完成其工作后收到GET)。

当脚本工作10秒(led1)时,这是一个问题,但是当脚本在无限循环(led2)中工作时是一个大问题 - 我不能破坏它直到第一个脚本停止(但它是True循环所以......)

我在第二个脚本中考虑Python的subprocess.check_call()来杀死第一个但它似乎没有帮助。

任何想法如何解决这个问题?

android python apache raspberry-pi apache2
1个回答
0
投票

最后,我设法做到了。杀死无限循环的想法是正确的,但首先我考虑在Python脚本上做错,这是错误的。

我注意到,当本地.php站点正在加载时(由于while循环),那些GET响应没有出现在日志中,因此需要中止当前的GET。它可以用Apache的httpcomponents完成。

所以经过一些修改我的代码看起来像这样:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
boolean flagToggle = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Switch switch_auto = findViewById(R.id.switch_auto);
    Switch switch_manual = findViewById(R.id.switch_manual);


    switch_auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        HttpGet requestOn = new HttpGet();
        HttpGet requestOff = new HttpGet();

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (!flagToggle) {
                requestOn = new HttpGet();
                requestOff = new HttpGet();
                flagToggle = true;
            }

            Background_get switchOn = new Background_get(requestOn);
            Background_get switchOff = new Background_get(requestOff);

            if (isChecked) {
                switchOn.execute("led1=1");
            }
            else {
                requestOn.abort();
                switchOff.execute("led1=0");
                flagToggle = false;
            }

        }
    });

    switch_manual.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        HttpGet requestOn = new HttpGet();
        HttpGet requestOff = new HttpGet();

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (!flagToggle) {
                requestOn = new HttpGet();
                requestOff = new HttpGet();
                flagToggle = true;
            }

            Background_get switchOn = new Background_get(requestOn);
            Background_get switchOff = new Background_get(requestOff);

            if (isChecked) {
                switchOn.execute("led2=1");
            }
            else {
                requestOn.abort();
                switchOff.execute("led2=0");
                flagToggle = false;
            }

        }
    });
}

public class Background_get extends AsyncTask<String, Void, String> {

    private HttpGet mRequest = new HttpGet();

    public Background_get(HttpGet newRequest) {
        mRequest = newRequest;
    }

    public HttpGet getRequest() {
        return mRequest;
    }

    public void setRequest(HttpGet newRequest) {
        mRequest = newRequest;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URI url = new URI("http://192.168.0.248/?" + params[0]);
            // HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            HttpClient httpclient = new DefaultHttpClient();
            getRequest().setURI(url);
            HttpResponse response = httpclient.execute(getRequest());

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder result = new StringBuilder();
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine).append("\n");

            in.close();
            // response.close();
            // httpclient.close();

            return result.toString();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return null;
    }
}
}

值得注意的事情:

  • 对于每个切换切换,它需要单独的请求(新的HttpGet())
  • 与BackgroundGet相同(因为它是AsyncTask)
  • flagToggle有助于多次切换切换
© www.soinside.com 2019 - 2024. All rights reserved.