0

I found that we can run Phpcgi on android by going to this site.I have created a web server in android it works fine and i have installed Php cgi and want to ask that how can i link both so that i can run php scripts as well as HTML pages.Any help will be appreciated.

Update:

In my Request Processor the out is sent like this:

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

guessConte....()

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();
         }
Prakhar
  • 2,270
  • 19
  • 26

1 Answers1

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.

Tseng
  • 61,549
  • 15
  • 193
  • 205
  • 1
    sir are u on facebook i have become a great fan of yours – Prakhar Aug 20 '13 at 14:44
  • can i do it like in my webserver requestprocessor check the extension of request if it is .php than execute your code will it work..and please reply i will accept your answere if u reply.and now i have upvoted it :) – Prakhar Aug 20 '13 at 14:52
  • You can read the answer on this SO question: http://stackoverflow.com/a/3571239/455493 In general on StackOverflow one uses one question for one problem and starts new ones when they are not completely related. – Tseng Aug 20 '13 at 15:04
  • You will have to initialize the `phpFile` variable before you do the extension check of course. Will it work? Try it out. But `Process` is the way to go to execute something that's not part of Andorid or it's framework. – Tseng Aug 20 '13 at 15:24
  • can u tell me about sending the response using outputstream – Prakhar Aug 20 '13 at 16:01
  • OutputSteam is used like every other Steam. You use the `write(...)` methods to write bytes to the stream and call flush. You can wrap `OutputSteam` around a `PrintWriter` to directly write `String`s into the stream. But you should really open a new question for it. On StackOverflow one questions is to focused on one problem. Also there are many other older question and answers and you can use search to find the answer and post your own question if you don't find any answers that correspond to your problem. StackOverflow is not like a forum for discussions. – Tseng Aug 20 '13 at 16:52