【问题标题】:Error: WAMP server and Android application?错误:WAMP 服务器和 Android 应用程序?
【发布时间】:2014-07-24 14:06:46
【问题描述】:

我正在尝试在我的计算机上运行一个开源应用程序,它将一个 android 应用程序连接到一个 WAMP 服务器。

这个应用程序绝对可以运行,因为我的一个同事可以在他们的计算机上运行它。但是,我无法让它在我的身上运行。

以下代码来自尝试注册用户以便他们随后可以登录的活动。但是,当我尝试运行它时出现 logcat 错误。有什么问题?

我已经正确连接了我的 WAMP 服务器(图标为绿色),并且还在 MYSQL 中创建了数据库。我知道这是可行的,因为我可以从 PHP 脚本填充 MYSQL 数据库。但是我似乎无法将其连接到 android 应用程序。

任何帮助都将不胜感激,我已经尝试了所有我能想到的方法来解决它。

Logcat 错误报告 1:

07-24 13:41:17.127: E/AndroidRuntime(1355): FATAL EXCEPTION: AsyncTask #1
07-24 13:41:17.127: E/AndroidRuntime(1355): Process: com.example.mysqltest, PID: 1355
07-24 13:41:17.127: E/AndroidRuntime(1355): java.lang.RuntimeException: An error occured while executing doInBackground()
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.lang.Thread.run(Thread.java:841)
07-24 13:41:17.127: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
07-24 13:41:17.127: E/AndroidRuntime(1355):     at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:109)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:1)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-24 13:41:17.127: E/AndroidRuntime(1355):     ... 4 more

Logcat 错误 2:

07-24 13:41:17.127: E/AndroidRuntime(1355): FATAL EXCEPTION: AsyncTask #1
07-24 13:41:17.127: E/AndroidRuntime(1355): Process: com.example.mysqltest, PID: 1355
07-24 13:41:17.127: E/AndroidRuntime(1355): java.lang.RuntimeException: An error occured while executing doInBackground()
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.lang.Thread.run(Thread.java:841)
07-24 13:41:17.127: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
07-24 13:41:17.127: E/AndroidRuntime(1355):     at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:109)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:1)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-24 13:41:17.127: E/AndroidRuntime(1355):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-24 13:41:17.127: E/AndroidRuntime(1355):     ... 4 more
07-24 13:41:17.699: E/WindowManager(1355): android.view.WindowLeaked: Activity com.example.mysqltest.Register has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52906bd4 V.E..... R......D 0,0-1026,288} that was originally added here
07-24 13:41:17.699: E/WindowManager(1355):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
07-24 13:41:17.699: E/WindowManager(1355):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
07-24 13:41:17.699: E/WindowManager(1355):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-24 13:41:17.699: E/WindowManager(1355):  at android.app.Dialog.show(Dialog.java:286)
07-24 13:41:17.699: E/WindowManager(1355):  at com.example.mysqltest.Register$CreateUser.onPreExecute(Register.java:86)
07-24 13:41:17.699: E/WindowManager(1355):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-24 13:41:17.699: E/WindowManager(1355):  at android.os.AsyncTask.execute(AsyncTask.java:535)
07-24 13:41:17.699: E/WindowManager(1355):  at com.example.mysqltest.Register.onClick(Register.java:68)
07-24 13:41:17.699: E/WindowManager(1355):  at android.view.View.performClick(View.java:4438)
07-24 13:41:17.699: E/WindowManager(1355):  at android.view.View$PerformClick.run(View.java:18422)
07-24 13:41:17.699: E/WindowManager(1355):  at android.os.Handler.handleCallback(Handler.java:733)
07-24 13:41:17.699: E/WindowManager(1355):  at android.os.Handler.dispatchMessage(Handler.java:95)
07-24 13:41:17.699: E/WindowManager(1355):  at android.os.Looper.loop(Looper.java:136)
07-24 13:41:17.699: E/WindowManager(1355):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 13:41:17.699: E/WindowManager(1355):  at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:41:17.699: E/WindowManager(1355):  at java.lang.reflect.Method.invoke(Method.java:515)
07-24 13:41:17.699: E/WindowManager(1355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 13:41:17.699: E/WindowManager(1355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 13:41:17.699: E/WindowManager(1355):  at dalvik.system.NativeStart.main(Native Method)

Android 活动:

public class Register extends Activity implements OnClickListener{

    private EditText user, pass;
    private Button  mRegister;

     // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php login script

    //localhost :  
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
   // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/register.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://192.168.56.1/webservice/register.php";

  //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/register.php";

    //ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        user = (EditText)findViewById(R.id.username);
        pass = (EditText)findViewById(R.id.password);


        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

                new CreateUser().execute();

    }

    class CreateUser extends AsyncTask<String, String, String> {

         /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Register.this);
            pDialog.setMessage("Creating User...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
             // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");

                //Posting user data to script 
                JSONObject json = jsonParser.makeHttpRequest(
                       LOGIN_URL, "POST", params);

                // full json response
                Log.d("Login attempt", json.toString());

                // json success element
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("User Created!", json.toString());                
                    finish();
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }


}

对应的PHP脚本:

<?php

/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script.  Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");

//if posted data is not empty
if (!empty($_POST)) {
    //If the username or password is empty when the user submits
    //the form, the page will die.
    //Using die isn't a very good practice, you may want to look into
    //displaying an error message within the form instead.  
    //We could also do front-end form validation from within our Android App,
    //but it is good to have a have the back-end code do a double check.
    if (empty($_POST['username']) || empty($_POST['password'])) {


        // Create some data that will be the JSON response 
        $response["success"] = 0;
        $response["message"] = "Please Enter Both a Username and Password.";

        //die will kill the page and not execute any code below, it will also
        //display the parameter... in this case the JSON data our Android
        //app will parse
        die(json_encode($response));
    }

    //if the page hasn't died, we will check with our database to see if there is
    //already a user with the username specificed in the form.  ":user" is just
    //a blank variable that we will change before we execute the query.  We
    //do it this way to increase security, and defend against sql injections
    $query        = " SELECT 1 FROM users WHERE username = :user";
    //now lets update what :user should be
    $query_params = array(
        ':user' => $_POST['username']
    );

    //Now let's make run the query:
    try {
        // These two statements run the query against your database table. 
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one to product JSON data:
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));
    }

    //fetch is an array of returned data.  If any data is returned,
    //we know that the username is already in use, so we murder our
    //page
    $row = $stmt->fetch();
    if ($row) {
        // For testing, you could use a die and message. 
        //die("This username is already in use");

        //You could comment out the above die and use this one:
        $response["success"] = 0;
        $response["message"] = "I'm sorry, this username is already in use";
        die(json_encode($response));
    }

    //If we have made it here without dying, then we are in the clear to 
    //create a new user.  Let's setup our new query to create a user.  
    //Again, to protect against sql injects, user tokens such as :user and :pass
    $query = "INSERT INTO users ( username, password ) VALUES ( :user, :pass ) ";

    //Again, we need to update our tokens with the actual data:
    $query_params = array(
        ':user' => $_POST['username'],
        ':pass' => $_POST['password']
    );

    //time to run our query, and create the user
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error2. Please Try Again!";
        die(json_encode($response));
    }

    //If we have made it this far without dying, we have successfully added
    //a new user to our database.  We could do a few things here, such as 
    //redirect to the login page.  Instead we are going to echo out some
    //json data that will be read by the Android application, which will login
    //the user (or redirect to a different activity, I'm not sure yet..)
    $response["success"] = 1;
    $response["message"] = "Username Successfully Added!";
    echo json_encode($response);

    //for a php webservice you could do a simple redirect and die.
    //header("Location: login.php"); 
    //die("Redirecting to login.php");


} else {
?>
    <h1>Register</h1> 
    <form action="register.php" method="post"> 
        Username:<br /> 
        <input type="text" name="username" value="" /> 
        <br /><br /> 
        Password:<br /> 
        <input type="password" name="password" value="" /> 
        <br /><br /> 
        <input type="submit" value="Register New User" /> 
    </form>
    <?php
}

?>

Logcat 错误:

07-24 19:16:30.584: E/BandwidthController(155): runIptablesCmd(): res=1 status=768 failed /system/bin/ip6tables -t raw -A bw_raw_PREROUTING -m owner --socket-exists
07-24 19:16:30.584: A/NetworkManagementService(554): problem enabling bandwidth controls
07-24 19:16:30.584: A/NetworkManagementService(554): com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '1 bandwidth enable' failed with '400 1 Bandwidth command failed'
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:391)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.NativeDaemonConnector.executeForList(NativeDaemonConnector.java:327)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:292)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.NetworkManagementService.prepareNativeDaemon(NetworkManagementService.java:348)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.NetworkManagementService.systemReady(NetworkManagementService.java:224)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.ServerThread$2.run(SystemServer.java:961)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.am.ActivityManagerService.systemReady(ActivityManagerService.java:9363)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.ServerThread.initAndLoop(SystemServer.java:938)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.server.SystemServer.main(SystemServer.java:1179)
07-24 19:16:30.584: A/NetworkManagementService(554):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:16:30.584: A/NetworkManagementService(554):    at java.lang.reflect.Method.invoke(Method.java:515)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 19:16:30.584: A/NetworkManagementService(554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 19:16:30.584: A/NetworkManagementService(554):    at dalvik.system.NativeStart.main(Native Method)
07-24 19:16:30.588: E/WifiStateMachine(554): Failed to reload STA firmware java.lang.IllegalArgumentException: command '2 softap fwreload eth1 STA' failed with '501 2 SoftAP command has failed'

【问题讨论】:

  • 你确定你电脑上的登录IP是一样的吗?这可能是不同的192.168.56.1/webservice/register.php,因为那是他的本地 IP,而你的可能不同......
  • 不,我改了他的IP地址,你看到的那个是我的
  • 如果您从浏览器中点击该网址,它可以访问吗?
  • 如果我这样做,我会得到错误:
  • 连接数据库失败:SQLSTATE[HY000] [1045] 用户'root'@'localhost'访问被拒绝(使用密码:YES)

标签: php android mysql wamp


【解决方案1】:

这是您的数据库凭据的问题。检查您的配置文件为 root 指定的密码,并确保它与您在本地使用的密码相同。

【讨论】:

  • 我的配置文件有以下内容: $username = "root"; $密码 = " "; $host = "本地主机"; $dbname = "rosslocalhost1";
  • 我需要将这些与什么进行比较?感谢您的帮助!
  • 确保有一个数据库名称 rosslocalhost1,并使用正确的登录名,所以如果您的 mysql root 用户 pw 为 YES,请在您的配置文件中设置为 YES
  • 我几乎可以肯定 mySql root 用户没有密码,我该如何检查?谢谢
  • 我以为你说你用的是密码yes什么的
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2012-01-25
  • 2017-07-17
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
相关资源
最近更新 更多