2

I need to use PHP script with text output in my app. I have following code:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class Fourth_Fragment extends Fragment {
    View myView4;
    //TextView textl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView4 = inflater.inflate(R.layout.fourth_layout, container, false);

        //textl = (TextView)myView4.findViewById(R.id.LoungeTest);
        //textl.setText(outputlog);
        final String url = "file:///android_asset/lounge.php";
        new Thread() {
            @Override
            public void run() {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = httpclient.execute(new HttpGet(url));
                    StatusLine statusLine = response.getStatusLine();
                    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        try {
                            response.getEntity().writeTo(out);
                            out.close();
                        } catch (IOException e) {
                        }
                        String responseString = out.toString();
                        //..more logic
                    } else {
                        //Closes the connection.
                        try {
                            response.getEntity().getContent().close();
                            throw new IOException(statusLine.getReasonPhrase());
                        } catch (IOException e) {
                        }
                    }
                }
                catch (ClientProtocolException e)
                {

                }
                catch (IOException e)
                {

                }
            }
        }.start();

        return myView4;
    }




}

And this is logcat:

11-01 19:47:24.784 1454-1454 W/System.err: java.io.IOException: Error running exec(). Command: [php, android.content.res.AssetManager$AssetInputStream@1e44a661] Working Directory: null Environment: null
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:211)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.Runtime.exec(Runtime.java:173)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.Runtime.exec(Runtime.java:246)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.Runtime.exec(Runtime.java:189)
11-01 19:47:24.794 1454-1454 W/System.err:     at myapp.test.Fourth_Fragment.execPHP(Fourth_Fragment.java:48)
11-01 19:47:24.794 1454-1454 W/System.err:     at myapp.test.Fourth_Fragment.onCreateView(Fourth_Fragment.java:33)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.os.Looper.loop(Looper.java:135)
11-01 19:47:24.794 1454-1454 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5253)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
11-01 19:47:24.794 1454-1454 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-01 19:47:24.794 1454-1454 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-01 19:47:24.794 1454-1454 W/System.err: Caused by: java.io.IOException: Permission denied
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.ProcessManager.exec(Native Method)
11-01 19:47:24.794 1454-1454 W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:209)
11-01 19:47:24.794 1454-1454 W/System.err:  ... 19 more

AndroidManifest:

...
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
...

So is there any way, how to execute PHP script (filce located in assets folder) in Android java App with text output (as string)? Thanks

2

2 Answers 2

2

how to execute PHP script (filce located in assets folder) in Android java App with text output (as string)?

Not really. Android does not have a PHP interpreter built into it. PHP is a technology usually used by Web servers; Android does not have a Web server built into it either.

You are welcome to try to find a PHP interpreter for Android, then try to figure out how to use it to execute your PHP script. Most likely, you will be better served by getting rid of the PHP script in the first place.

Sign up to request clarification or add additional context in comments.

Comments

-1

First of all, I think your URL is wrong,

final String url = "file:///android_asset/lounge.php";

It must be like

final String url = "http://127.0.0.1/android_asset/lounge.php"; 

where 127.0.0.1 is ip used for localhost.

for local system.

2 Comments

Please explain how using a localhost URL will allow the OP to execute a PHP script, contained in assets/, on an Android device.
It's impossible to run a script present in asset folder. Your script must be at server folder to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.