Click here to Skip to main content
11,583,859 members (66,369 online)
Click here to Skip to main content

Connecting your Android app to your WAMP server

, 25 Jul 2014 CPOL 31.2K 11
Rate this:
Please Sign up or sign in to vote.
Android at figure tips

Introduction

This tutorial is a brief guide on how to intergrate your Android App to Wamp Server : this could be hosted on you local machine or on a remote server.

Background

Android has an inbuilt database called SQLITE which is shipped with Android but the problem with it is that the people that developed were not thinking Objects(OOP) yet today's progamming revolves around use of Objects(OOP) therefore using MYSQL will offer you a better OOP style and abstration thus quicker  response to complex queries

The other point is for security purposes ;having a database in SQL provides better security plus more storage 

Using the code

Below is the the entire sample source code but before you deploy this code ensure the following;

  1. Set you Apache httpd.conf file to accept connections from all services/connections i.e   Allow from all
  2. Ensure that you using WAMPSERVER 2.4 ,Apache/2.4.4 and PHP/5.4.16 that supports  PHP Data Objects ( PDO)http://www.wampserver.com/en/
  3. For the URL please use a remote ip address i.e 192.168.0.1/jsondemo.register.php.Don't use you local address like 127.0.0.1/register.php or localhost/register.php because the application will always crush therefore ensure that you pc is connected to a network,internet so that it can attain a valid ip address
  4. Put the php code into your wamp  www directory
  5. And then import the sample database into you server

 

Below is the xml file that collects informations about a user to be registered.Keeping all things basic

//
// <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <!-- firstname -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="FirstName:"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17sp"/>
    
    <!-- firstname input -->
    <EditText android:id="@+id/fname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:layout_marginBottom="15dip"
        android:singleLine="true"/>
    
     <!-- lastname -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LastName:"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17sp"/>
    
    <!-- lastname input -->
    <EditText android:id="@+id/lname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginBottom="15dip"
        android:singleLine="true"/>
    
     <!-- username -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username:"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17sp"/>
    
    <!-- username input -->
    <EditText android:id="@+id/uname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginBottom="15dp"
        android:singleLine="true"/>
    
    <!-- password -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Password:"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17dip"/>
    
    <!-- password input -->
    <EditText android:id="@+id/pass" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginBottom="15dp"
        android:singleLine="true"
        android:inputType="textPassword"/>
    
    
     <!-- address -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Address"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17sp"/>
    
    <!-- address input -->
    <EditText android:id="@+id/addr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:layout_marginBottom="15dip"
        android:singleLine="true"/>
    
     <!-- contact -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Contact:"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textSize="17sp"/>
    
    <!-- contact input -->
    <EditText android:id="@+id/contact"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginBottom="15dp"
        android:singleLine="true"/>
    
    <!-- Button Register -->
    <Button android:id="@+id/btnreg" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>
    
    
    <!-- Button Cancel -->
    <Button android:id="@+id/btncancel" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"/>
    
</LinearLayout>
</ScrollView>
//

 

Below is the  JSONParser class that sends an http request in form of a POST or GET method plus also  getting the JSON data sent by the MYSQL queries

//

package com.code256.jsonmysql;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser{

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {
            
            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                
            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }            
            

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
    
    public JSONObject getJSONFromUrl(final String url) {

        // Making HTTP request
        try {
            // Construct the client and the HTTP request.
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            // Execute the POST request and store the response locally.
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // Extract data from the response.
            HttpEntity httpEntity = httpResponse.getEntity();
            // Open an inputStream with the data content.
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // Create a BufferedReader to parse through the inputStream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            // Declare a string builder to help with the parsing.
            StringBuilder sb = new StringBuilder();
            // Declare a string to store the JSON object data in string form.
            String line = null;
            
            // Build the string until null.
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            
            // Close the input stream.
            is.close();
            // Convert the string builder data to an actual string.
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // Try to parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // Return the JSON Object.
        return jObj;

    }
}
//
//

Below is a class that interacts with the our  layout reg_activity.xml.Am using the AsyncTask<String, String, String>  helper class that performs background operations and publish the results in the UI thread without having to manipulate the thread or other handlers. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

 

 

//

package com.code256.jsonmysql;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegActivity extends Activity {

    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText fname;
    EditText lname;
    EditText username;
    EditText password;
    EditText location;
    EditText contact;
    
    Button btnreg;
    Button btncancel;

    // url to create new product
    private static String url_new_user = "http://172.16.3.82/jsonmysqldemo/new_user.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_activity);

        // Edit Text
        fname = (EditText) findViewById(R.id.fname);
        lname = (EditText) findViewById(R.id.lname);
        username = (EditText) findViewById(R.id.uname);
        password = (EditText) findViewById(R.id.pass);
        location = (EditText) findViewById(R.id.addr);
        contact = (EditText) findViewById(R.id.contact);
        

        // Create button
        btnreg = (Button) findViewById(R.id.btnreg);
        btncancel = (Button) findViewById(R.id.btncancel);

        // button click event
        btnreg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new CreateNewProduct().execute();
            }
        });
        
        btncancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                finish();
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        });
    }

    /**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RegActivity.this);
            pDialog.setMessage("Registering New User..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String Firstname = fname.getText().toString();
            String Lastname = lname.getText().toString();
            String Username = username.getText().toString();
            String Password = password.getText().toString();
            String Address = location.getText().toString();
            String Contact = contact.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("fname", Firstname));
            params.add(new BasicNameValuePair("lname", Lastname));
            params.add(new BasicNameValuePair("username", Username));
            params.add(new BasicNameValuePair("password", Password));
            params.add(new BasicNameValuePair("location", Address));
            params.add(new BasicNameValuePair("contact", Contact));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_new_user,
                    "POST", params);
            
            // check log cat from response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created a user
                    Intent i = new Intent(getApplicationContext(), RegActivity.class);
                    startActivity(i);
                    
                    // closing this screen
                    finish();
                } else {
                    // failed to create user
                    Log.d("failed to create user", json.toString());

                }
            } 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 done
            pDialog.dismiss();
        }

    }
}

Below is the  manifest xml file..all things are basic except that we need some permissions of internet because this simple app will need to access the internet

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.code256.jsonmysql.RegActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Below is the a PDO php class that will validate all our POST and GET request so that they return  well  formatted JSON arrays and avoid SQL injection and Cross Site Scripting.  

<?php 

    // These variables define the connection information for your MySQL database 
    // This is also for the Xampp example,  if you are hosting on your own server,
    //make the necessary changes (mybringback_travis, etc.)
    $username = "root"; 
    $password = ""; 
    $host = "localhost"; 
    $dbname = "safari256"; 

    // UTF-8 is a character encoding scheme that allows you to conveniently store 
    // a wide varienty of special characters, like &cent; or &euro;, in your database. 
    // By passing the following $options array to the database connection code we 
    // are telling the MySQL server that we want to communicate with it using UTF-8 
    // See Wikipedia for more information on UTF-8: 
    // http://en.wikipedia.org/wiki/UTF-8 
    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
     
    // A try/catch statement is a common method of error handling in object oriented code. 
    // First, PHP executes the code within the try block.  If at any time it encounters an 
    // error while executing that code, it stops immediately and jumps down to the 
    // catch block.  For more detailed information on exceptions and try/catch blocks: 
    // http://us2.php.net/manual/en/language.exceptions.php 
    try 
    { 
        // This statement opens a connection to your database using the PDO library 
        // PDO is designed to provide a flexible interface between PHP and many 
        // different types of database servers.  For more information on PDO: 
        // http://us2.php.net/manual/en/class.pdo.php 
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
    } 
    catch(PDOException $ex) 
    { 
        // If an error occurs while opening a connection to your database, it will 
        // be trapped here.  The script will output an error and stop executing. 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code 
        // (like your database username and password). 
        die("Failed to connect to the database: " . $ex->getMessage()); 
    } 
     
    // This statement configures PDO to throw an exception when it encounters 
    // an error.  This allows us to use try/catch blocks to trap database errors. 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     
    // This statement configures PDO to return database rows from your database using an associative 
    // array.  This means the array will have string indexes, where the string value 
    // represents the name of the column in your database. 
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     
    // This block of code is used to undo magic quotes.  Magic quotes are a terrible 
    // feature that was removed from PHP as of PHP 5.4.  However, older installations 
    // of PHP may still have magic quotes enabled and this code is necessary to 
    // prevent them from causing problems.  For more information on magic quotes: 
    // http://php.net/manual/en/security.magicquotes.php 
    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
    { 
        function undo_magic_quotes_gpc(&$array) 
        { 
            foreach($array as &$value) 
            { 
                if(is_array($value)) 
                { 
                    undo_magic_quotes_gpc($value); 
                } 
                else 
                { 
                    $value = stripslashes($value); 
                } 
            } 
        } 
     
        undo_magic_quotes_gpc($_POST); 
        undo_magic_quotes_gpc($_GET); 
        undo_magic_quotes_gpc($_COOKIE); 
    } 
     
    // This tells the web browser that your content is encoded using UTF-8 
    // and that it should submit content back to you using UTF-8 
    header('Content-Type: text/html; charset=utf-8'); 
     
    // This initializes a session.  Sessions are used to store information about 
    // a visitor from one web page visit to the next.  Unlike a cookie, the information is 
    // stored on the server-side and cannot be modified by the visitor.  However, 
    // note that in most cases sessions do still use cookies and require the visitor 
    // to have cookies enabled.  For more information about sessions: 
    // http://us.php.net/manual/en/book.session.php 
    session_start(); 

    // Note that it is a good practice to NOT end your PHP files with a closing PHP tag. 
    // This prevents trailing newlines on the file from being included in your output, 
    // which can cause problems with redirecting users.


?>

Below is a php class to perform the basic MYSQL functions i.e CREATE,UPDATE,DELETE and COUNT the number of rows affected by a query

<?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 __DIR__ . '/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();
    }

}

?>

Below is the a php code that use create a new user and insert them into our database

<?php

/*
 * Following code will create a new user row
 * All user details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['location']) && isset($_POST['contact'])) {
    
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $location = $_POST['location'];
    $contact = $_POST['contact'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO users( username, password,first_name, last_name, location, contact) VALUES( '$username','$password','$fname', '$lname','$location','$contact')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "User successfully Registered.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
        
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

Use the "var" button to to wrap Variable or class names in <code> tags like this.

Points of Interest

This  source code only works if your are familiar with the three technologies i.e JAVA,PHP/MYSQL

Using the emulator is slow but saves alot of time trying to have the phone and Your local computer using the same ip address.With the emulator you only have to ensure that the URL to pick JSON string from your local machine has the same address as your ip address

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionquestion on httpd.conf Pin
Member 115109489-Mar-15 9:19
memberMember 115109489-Mar-15 9:19 
QuestionThe tutorial is really good. Pin
mayurthole21-Nov-14 0:21
groupmayurthole21-Nov-14 0:21 
QuestionPDO vs mysql_connect Pin
Member 1110374123-Sep-14 4:21
memberMember 1110374123-Sep-14 4:21 
GeneralMy vote of 5 Pin
Mihai MOGA13-Aug-14 3:21
professionalMihai MOGA13-Aug-14 3:21 
GeneralAndroid Tutorial Contest Entry? Pin
Kevin Priddle31-Jul-14 10:50
staffKevin Priddle31-Jul-14 10:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 25 Jul 2014
Article Copyright 2014 by henry sentio kauta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid