Android SDK - App不读取php / MySQL

时间:2013-04-15 13:33:45

标签: php android mysql sdk

正如我在标题中所说的

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.achkars.espaceado"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.achkars.espaceado.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.FullScreen"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

package com.achkars.espaceado;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends Activity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webview = new WebView(this);
         setContentView(webview);
         webview.getSettings().setJavaScriptEnabled(true);
         webview.setWebChromeClient(new WebChromeClient());
        // Simplest usage: note that an exception will NOT be thrown
         // if there is an error loading this page (see below).
         webview.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

文件News.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="file:///android_asset/style.css" media="screen"/>
</head>
<body>
<img alt="full screen background image" src="file:///android_asset/back.jpg" id="full-screen-background-image" />
<center><img alt="up" src="file:///android_asset/top.png" id="up" /> </center>
<center><img alt="down" src="file:///android_asset/down.png" id="down"/></center>
<h2><center>
Les nouvelles (Blagues)
</center></h2>
<ul>
<a href="file:///android_asset/index.html"><li class="arrow">La page precedente</li></a>
</ul>
<ul>
<?php
$con = mysql_connect("The Domain","Username","Password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DB Name", $con);

$result = mysql_query("SELECT * FROM espace");

while($row = mysql_fetch_array($result))
  {
echo "<a href=".$row['link'].">","<li class=".$row['arrow'].">";
echo $row['news'];
echo "</li></a>";
  };
mysql_close($con);
?>
</ul>
</body>
</html>

在PC上它正常工作,但我正在接听电话:

"," ";echo $row['news'}; echo " 
";}; mysql_close($con); ?>

是否需要任何许可?

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

Android无法在设备上运行PHP代码。 PHP是一种服务器端语言,您必须将所有PHP功能移动到Web服务器上,并通过它访问它。

请注意,您可以使用PHP for Android项目在本地运行代码,但我不确定该端口的完整性或活动性。

答案 1 :(得分:1)

php页面是一个脚本,需要解析PHP解释器并正确显示。你不能在Android中使用它(这不是你真正想要的)。您需要将此脚本存储在具有PHP兼容性的Web服务器中,并通过手机浏览器通过互联网查看(再次,我认为这不是您想要的)

答案 2 :(得分:0)

获取Android的Web服务器,将您的php脚本移动到htdocs(或其他)文档文件夹,当localhost:8080/调用时,一切运行正常。我在智能手机和平板电脑上使用KSWeb服务器没有任何问题。