I made a basic login app using online SQLite database(http://demo3534535.16mb.com) It works fine on online server. Now i want to run on local server using WAMP in Gennymotion emulator.
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
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
<?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
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
if you're going local, follow these steps:
You need to create a 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.
Now type ipconfig
in the command line, so that you discover your machine's IPv4 address:
EDIT:
Make sure you are noting your Wireless LAN adapter's IPv4 address.
Note this IP address. This is the one you are going to use as the HOST address.
The Configuration part.
Change define('HOST','mysql.hostinger.in');
to this 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.
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). Change REGISTER_URL
's value to this:
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.