简体   繁体   English

应用程序不能在 WAMP 上运行,但可以在在线服务器上运行

[英]App is not working on WAMP but It works in online server

I made a basic login app using online SQLite database( http://demo3534535.16mb.com ) It works fine on online server.我使用在线 SQLite 数据库( http://demo3534535.16mb.com )制作了一个基本的登录应用程序,它在在线服务器上运行良好。 Now i want to run on local server using WAMP in Gennymotion emulator.现在我想在 Gennymotion 模拟器中使用 WAMP 在本地服务器上运行。

What change in code i have to do?我必须做哪些代码更改? My working online code is:我的在线工作代码是:

dbconnect.php数据库连接文件

<?php
    define('HOST','mysql.hostinger.in');
    define('USER','u756_userd');
    define('PASS','pass345');
    define('DB','u754113_demo');

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

MainActivity.java主活动.java

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {


  private static final String REGISTER_URL = "http://192.168.1.102/User/Register.php";

    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_EMAIL = "email";


    private EditText editTextUsername;
    private EditText editTextEmail;
    private EditText editTextPassword;

    private Button buttonRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        editTextEmail= (EditText) findViewById(R.id.editTextEmail);

        buttonRegister = (Button) findViewById(R.id.buttonRegister);

        buttonRegister.setOnClickListener(this);
    }

    private void registerUser(){
        final String username = editTextUsername.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        if(v == buttonRegister){
            registerUser();
        }
    }
}

db_connect.php db_connect.php

<?php
    define('HOST','localhost');
    define('USER','');
    define('PASS','');
    define('DB','db_demo');

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

Register.php注册.php

<?php

    if($_SERVER['REQUEST_METHOD']=='POST'){

        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        require_once('dbConnect.php');

        $sql = "INSERT INTO volley (username,password,email) VALUES ('$username','$email','$password')";


        if(mysqli_query($con,$sql)){
            echo "Successfully Registered";
        }else{
            echo "Could not register";

        }
    }else{
echo 'error';
}

I am using internet via wifi我正在通过 wifi 上网

if you're going local, follow these steps:如果您要去本地,请按照以下步骤操作:

  1. You need to create a WLAN.您需要创建一个 WLAN。 Open the command prompt Admin mode and type these commands:打开命令提示符管理模式并键入以下命令:

     netsh wlan set hostednetwork mode=allow ssid=DevelopmentLAN key=yourPassword

    Press OK.按确定。 Then execute the following command:然后执行以下命令:

     netsh wlan start hostednetwork

This shall start your WLAN .这将启动您的 WLAN

Now type ipconfig in the command line, so that you discover your machine's IPv4 address:现在在命令行中输入ipconfig ,以便您发现您机器的 IPv4 地址:

配置文件

EDIT:编辑:

Make sure you are noting your Wireless LAN adapter 's IPv4 address.确保您记录了无线 LAN 适配器的 IPv4 地址。

Note this IP address.记下这个 IP 地址。 This is the one you are going to use as the HOST address.这是您将用作主机地址的地址。

The Configuration part.配置部分。

Change define('HOST','mysql.hostinger.in');更改define('HOST','mysql.hostinger.in');

to this define('HOST','localhost');为此define('HOST','localhost');

As for the rest, you need to change the user's name, password and the database name to your WAMPServer 's username and password and your database 's name on WAMP, respectively.至于其余的,您需要将用户名、密码和数据库名称分别更改为您的WAMPServer的用户名和密码以及您在 WAMP 上的数据库名称。

Now I will suppose that your register.php file is wrapped inside a User folder in your WAMP projects directory (C:/wamp/www/User/register.php).现在我假设您的register.php文件包含在 WAMP 项目目录 (C:/wamp/www/User/register.php) 中的User文件夹中。 Change REGISTER_URL 's value to this:REGISTER_URL的值更改为:

private static final String REGISTER_URL = "http://192.168.1.3/User/register.php";

And that's it, you're ready to go.就是这样,你准备好了。 Finally make sure your firewall is OFF , otherwise it will block your genymotion emulator from connecting to the WAMP server on your machine, and that you change 192.168.1.3 to your machine's IP address.最后确保您的防火墙关闭,否则它会阻止您的 genymotion 模拟器连接到您机器上的 WAMP 服务器,并且您将192.168.1.3更改为您机器的 IP 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM