0

我发现我们可以通过访问此站点在 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();
         }
4

1 回答 1

2

You can use the Process and ProcessBuilder classes to create and execute an command. Keep in mind that depending on the process you want to execute, you may require root permissions and it won't work on non-rooted Android devices.

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

Don't be confused by the naming. According to the Process documentation getInputStream() returns the output of the stream connected to the std::out. This will return Code (Json, HTML, plain text) generated by the PHP.

However, chances are you will need root for it to work. Or that the files won't have execution permission (x in Linux) when you unpack them from your APK. But calling chmod or chown (if it's assigned to the wrong user name) will most likely require an rooted Android devices.

于 2013-08-20T14:40:08.507 回答