从模拟器上运行的Android应用程序联系本地Web服务

时间:2014-03-19 10:34:50

标签: android web-services android-emulator

我创建了一个REST Web服务(使用Web API),但我还没有发布它。我想在测试与Android客户端的通信之前。所以我暂时在我的本地机器上发布了ws,在同一台机器上,我启动了运行我的测试应用程序的模拟器。

是否可以通过模拟器和本地ws中运行的应用进行通信?

2 个答案:

答案 0 :(得分:0)

Your qestion is not cleared to me.If you make your webservice and place your webservice locally(using wamp or xamp) then you can communicate to your WS via JSON parsing.

You should create DB_CONFIG file

define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "The name of your database"); // database name
define('DB_SERVER', "localhost"); // db server
?>

and one DB_CONNECT file


<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once 'db_config.php';

        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}

?>


Then you can write individual php file where you can encode json and fetch the data through the url.You will also need a jsonparser class(which will connect the url with the ws) in your application.

答案 1 :(得分:-1)

localhost指的是运行代码的设备,在本例中是模拟器。

如果要引用运行Android模拟器的计算机,请改用IP地址10.0.2.2。您可以从... here

了解更多信息