Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中使用php cgi和Web服务器_Java_Android_Sockets_Webserver_Php - Fatal编程技术网

Java 在android中使用php cgi和Web服务器

Java 在android中使用php cgi和Web服务器,java,android,sockets,webserver,php,Java,Android,Sockets,Webserver,Php,我发现我们可以在android上运行Phpcgi。我已经在android上创建了一个web服务器,它运行得很好,我已经安装了Php cgi,我想问一下如何将两者链接起来,以便我可以运行Php脚本和HTML页面。任何帮助都将不胜感激。 更新: 在我的请求处理器中,输出如下所示: contentType = guessContentTypeFromName(filename); Date now = new Date( ); out.write("Date: " + now + "\r\n");

我发现我们可以在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( );


您可以使用
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();
}
不要被命名搞混了。根据
过程
文档
getInputStream()
返回连接到
std::out
的流的输出。这将返回PHP生成的代码(Json、HTML、纯文本)

然而,你很可能需要根才能让它工作。或者,当您从APK解包文件时,这些文件将没有执行权限(Linux中的x)。但是调用
chmod
chown
(如果分配给了错误的用户名)很可能需要一个根安卓设备

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

先生,你在facebook上吗?我已经成为你的超级粉丝扫描我像在我的Web服务器上一样执行请求处理器检查请求的扩展名如果是.php,然后执行你的代码。请回答如果你回答,我会接受你的回答。现在我对它投了更高的票:)你可以阅读这个问题的答案:一般来说StackOverflow one对一个问题使用一个问题,并在问题不完全相关时启动新问题。当然,在进行扩展检查之前,您必须初始化
phpFile
变量。行吗?试试看。但是
Process
是执行不属于android或其框架的内容的方法。您能告诉我如何使用outputstream发送响应吗