我需要在我的應用程序中使用帶有文本輸出的PHP腳本。 我有以下代碼:Android - 從資產執行php腳本
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;
}
}
這是logcat的:
11-01 19:47:24.784 1454-1454 W/System.err: java.io.IOException: Error running exec(). Command: [php, [email protected]] 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>
...
那麼有沒有什麼辦法,如何執行PHP腳本(filce位於資產文件夾)在Android的Java應用程序與文本輸出(字符串)?由於
檢查我的答案在這裏:http://stackoverflow.com/questions/33462560/app-is-not-working-on-wamp-but-it-works-in-online-server/33462732#33462732 –
@DeveloperX它應該如何幫助? – tonakriz