我发现我们可以通过访问此 site 在 Android 上运行 Phpcgi我已经在android中创建了一个Web服务器,它工作正常,并且我已经安装了Php cgi,并且想问我如何链接两者,以便我可以运行php脚本以及HTML页面。任何帮助将不胜感激。
更新:
在我的请求处理器中,输出是这样发送的:
contentType = guessContentTypeFromName(filename);
Date now = new Date( );
out.write("Date: " + now + "\r\n");
out.write("Server: JHTTP/1.0\r\n");
out.write("Content-length: " + theData.length + "\r\n");
out.write("Content-type: " + contentType + "\r\n\r\n");
out.flush( );
猜猜孔蒂......()
if (name.endsWith(".php")) {
String pathToPhpExecutable = Environment.getExternalStorageDirectory() + "/data" + "/php-cgi";
String phpFile ="" + "/php/myPhpFile.php";
Process process = null;
try {
process = new ProcessBuilder()
.command(pathToPhpExecutable, phpFile)
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
} finally {
process.destroy();
}
最佳答案
您可以使用Process
和ProcessBuilder
类来创建和执行命令。请记住,根据您要执行的进程,您可能需要 root 权限,并且它无法在未 root 的 Android 设备上运行。
String pathToPhpExecutable = getFileDir() + "/php-cgi";
String phpFile = getFileDir() + "/php/myPhpFile.php";
Process process = new ProcessBuilder()
.command(pathToPhpExecutable, phpFile)
.redirectErrorStream(true)
.start();
try {
InputStream in = process.getInputStream();
// Read the input stream and i.e. display the results in a WebView
} finally {
process.destroy();
}
不要对命名感到困惑。根据 Process
文档,getInputStream()
返回连接到 std::out
的流的输出。这将返回 PHP 生成的代码(Json、HTML、纯文本)。
但是,您很可能需要 root 才能使其工作。或者,当您从 APK 中解压这些文件时,这些文件将没有执行权限(Linux 中的 x)。但调用 chmod
或 chown
(如果分配给错误的用户名)很可能需要 root 的 Android 设备。
关于java - 在 android 中使用 php-cgi 和网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337415/