• We just launched and are currently in beta. Join us as we build and grow the community.

Connect To PHP Server Using Android

Diszy

Performance Debugging Pro
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
138
Likes
10
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Operating System

Android

In this tutorial we will try to create a Connect To PHP Server using Android. Android is an open source so that developer find it easy to develop and expand new features. It used in several gadget like smartphone, tablet, and even television. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So let's do the coding..

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Then you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

Creating the database

Open your database web server then create a database name in it db_contact, after that click Import then locate the database file inside the folder of the application then click ok.
2018-05-08_16_09_38-localhost_127.0.0.1_db_contact_phpmyadmin_4.7.0.png

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_contact"

    )

    ;
  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Fatal Error: Connect Error!"

    )

    ;
  5. }
  6. ?>

Creating PHP Main Script

This code contains the main function of the php server. This code will try to access the php server to get the data from the server base on the data that has been inputed. To do that open any kind of text editor(notepadd++, etc..), then copy it inside the text editor and save it as index.php
  1. <?php
  2. require_once

    "conn.php"

    ;

  3. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  4. $password

    =

    $_POST

    [

    'password'

    ]

    ;

  5. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member` WHERE `username` = '$username

    ' && `password` = '$password

    '"

    )

    ;

  6. $row

    =

    $query

    ->

    num_rows

    ;

  7. if

    (

    $row

    >

    0

    )

    {
  8. echo

    "Login Successful"

    ;
  9. }

    else

    {
  10. echo

    "Error Login!"

    ;
  11. }
  12. ?>

Now that we're done with PHP, next is we will need to create the application to run the php server in the Android Platform.

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    manifest xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. package

    =

    "com.razormist.connecttophpserver"

    >
  4. <

    uses-

    permission android:

    name=

    "android.permission.INTERNET"

    ></

    uses-

    permission>
  5. <

    application
  6. android:

    allowBackup=

    "true"
  7. android:

    icon=

    "@mipmap/ic_launcher"
  8. android:

    label=

    "@string/app_name"
  9. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  10. android:

    supportsRtl=

    "true"
  11. android:

    theme=

    "@Style/AppTheme"

    >
  12. <

    activity android:

    name=

    ".MainActivity"
  13. android:

    configChanges=

    "orientation"
  14. android:

    screenOrientation=

    "portrait"

    >
  15. <

    intent-

    filter>
  16. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  17. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  18. </

    intent-

    filter>
  19. </

    activity>
  20. </

    application>

  21. </

    manifest>

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    RelativeLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. xmlns:

    app=

    "http://schemas.android.com/apk/res-auto"
  4. xmlns:

    tools=

    "http://schemas.android.com/tools"
  5. android:

    layout_width=

    "match_parent"
  6. android:

    layout_height=

    "match_parent"
  7. tools:

    context=

    "com.razormist.connecttophpserver.MainActivity"

    >


  8. <

    EditText
  9. android:

    id=

    "@+id/et_username"
  10. android:

    layout_width=

    "wrap_content"
  11. android:

    layout_height=

    "wrap_content"
  12. android:

    layout_centerHorizontal=

    "true"
  13. android:

    layout_marginTop=

    "120dp"
  14. android:

    ems=

    "10"
  15. android:

    inputType=

    "text"
  16. android:

    hint=

    "Username"

    />


  17. <

    EditText
  18. android:

    id=

    "@+id/et_password"
  19. android:

    layout_width=

    "wrap_content"
  20. android:

    layout_height=

    "wrap_content"
  21. android:

    layout_centerHorizontal=

    "true"
  22. android:

    layout_marginTop=

    "30dp"
  23. android:

    ems=

    "10"
  24. android:

    layout_below=

    "@+id/et_username"
  25. android:

    inputType=

    "textPassword"
  26. android:

    hint=

    "Password"

    />

  27. <

    Button


  28. android:

    id=

    "@+id/btn_login"
  29. android:

    layout_height=

    "wrap_content"
  30. android:

    layout_width=

    "wrap_content"
  31. android:

    layout_centerHorizontal=

    "true"
  32. android:

    layout_marginTop=

    "30dp"
  33. android:

    layout_below=

    "@+id/et_password"
  34. android:

    text=

    "Login"

    />


  35. </

    RelativeLayout>

Creating the Server Handler

This code contains the handler for accessing the PHP server. This code will try to create a url to access the php server by sending a POST data within the android application.To do that create a new class then called it as ServerHandler. And write these block of codes inside it to be able to generate a url link to the server.
  1. public

    class

    ServerHandler extends

    AsyncTask<

    String

    , Void

    , String>

    {
  2. Context

    context;
  3. AlertDialog.Builder

    builder;

  4. ServerHandler(

    Context

    context)

    {
  5. this

    .context

    =

    context;
  6. }

  7. @Override
  8. protected

    String

    doInBackground(

    String

    ... params

    )

    {
  9. String

    type =

    params[

    0

    ]

    ;
  10. String

    main_url =

    "http://192.168.137.157/Connect to PHP server/index.php"

    ;
  11. if

    (

    type.equals

    (

    "login"

    )

    )

    {
  12. try

    {
  13. String

    username =

    params[

    1

    ]

    ;
  14. String

    password =

    params[

    2

    ]

    ;
  15. URL

    url =

    new

    URL

    (

    main_url)

    ;
  16. HttpURLConnection

    httpURLConnection =

    (

    HttpURLConnection

    )

    url.openConnection

    (

    )

    ;
  17. httpURLConnection.setRequestMethod

    (

    "POST"

    )

    ;
  18. httpURLConnection.setDoOutput

    (

    true

    )

    ;
  19. httpURLConnection.setDoInput

    (

    true

    )

    ;
  20. OutputStream

    outputStream =

    httpURLConnection.getOutputStream

    (

    )

    ;
  21. BufferedWriter

    bufferedWriter =

    new

    BufferedWriter

    (

    new

    OutputStreamWriter

    (

    outputStream, "UTF-8"

    )

    )

    ;
  22. String

    post_data =

    URLEncoder

    .encode

    (

    "username"

    , "UTF-8"

    )

    +

    "="

    +

    URLEncoder

    .encode

    (

    username, "UTF-8"

    )

    +

    "&"

    +

    URLEncoder

    .encode

    (

    "password"

    , "UTF-8"

    )

    +

    "="

    +

    URLEncoder

    .encode

    (

    password, "UTF-8"

    )

    ;
  23. bufferedWriter.write

    (

    post_data)

    ;
  24. bufferedWriter.flush

    (

    )

    ;
  25. bufferedWriter.close

    (

    )

    ;
  26. outputStream.close

    (

    )

    ;
  27. InputStream

    inputStream =

    httpURLConnection.getInputStream

    (

    )

    ;
  28. BufferedReader

    bufferedReader =

    new

    BufferedReader

    (

    new

    InputStreamReader

    (

    inputStream, "iso-8859-1"

    )

    )

    ;
  29. String

    result=

    ""

    ;
  30. String

    line;
  31. while

    (

    (

    line =

    bufferedReader.readLine

    (

    )

    )

    !=

    null

    )

    {
  32. result +=

    line;
  33. }
  34. bufferedReader.close

    (

    )

    ;
  35. inputStream.close

    (

    )

    ;
  36. httpURLConnection.disconnect

    (

    )

    ;
  37. return

    result;

  38. }

    catch

    (

    MalformedURLException

    e)

    {
  39. e.printStackTrace

    (

    )

    ;
  40. }

    catch

    (

    IOException

    e)

    {
  41. e.printStackTrace

    (

    )

    ;
  42. }

  43. }
  44. return

    null

    ;
  45. }

  46. @Override
  47. protected

    void

    onPreExecute(

    )

    {
  48. builder =

    new

    AlertDialog.Builder

    (

    context)

    ;
  49. builder.setTitle

    (

    "System Information"

    )

    ;

  50. }

  51. @Override
  52. protected

    void

    onPostExecute(

    String

    result)

    {
  53. builder.setMessage

    (

    result)

    ;
  54. builder.setCancelable

    (

    false

    )

    ;
  55. builder.setPositiveButton

    (

    "YES"

    , new

    DialogInterface.OnClickListener

    (

    )

    {
  56. public

    void

    onClick(

    DialogInterface dialog, int

    id)

    {
  57. //do things
  58. }
  59. }

    )

    ;

  60. AlertDialog alert =

    builder.create

    (

    )

    ;
  61. alert.show

    (

    )

    ;
  62. }

  63. @Override
  64. protected

    void

    onProgressUpdate(

    Void

    ... values

    )

    {
  65. super

    .onProgressUpdate

    (

    values)

    ;
  66. }
  67. }

Note:To access the localhost server make sure you change the
main_url same as your machine local IP address.

The Main Function

This code contains the main function of the application. This code will send the information of user to the PHP server when the button is clicked. To start with first locate your MainActivity java file and open it, then write these variable inside the MainActivity class.
  1. EditText et_username, et_password;
  2. Button

    btn_login;

Finally, initialize the require methods inside the onCreate method to run the application.
  1. et_username =

    (

    EditText)

    findViewById(

    R.id

    .et_username

    )

    ;
  2. et_password =

    (

    EditText)

    findViewById(

    R.id

    .et_password

    )

    ;
  3. btn_login =

    (

    Button

    )

    findViewById(

    R.id

    .btn_login

    )

    ;


  4. btn_login.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  5. @Override
  6. public

    void

    onClick(

    View

    view)

    {
  7. String

    username =

    et_username.getText

    (

    )

    .toString

    (

    )

    ;
  8. String

    password =

    et_password.getText

    (

    )

    .toString

    (

    )

    ;

  9. if

    (

    !

    username.equals

    (

    ""

    )

    ||

    !

    password.equals

    (

    ""

    )

    )

    {
  10. String

    condition =

    "login"

    ;
  11. ServerHandler serverHandler =

    new

    ServerHandler(

    MainActivity.this

    )

    ;
  12. serverHandler.execute

    (

    condition, username, password)

    ;
  13. et_username.setText

    (

    ""

    )

    ;
  14. et_password.setText

    (

    ""

    )

    ;
  15. }

    else

    {
  16. Toast.makeText

    (

    MainActivity.this

    , "Required Field!"

    , Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  17. }

  18. }
  19. }

    )

    ;

Try to run the app and see if it worked.

There you have it we have created a Connect To PHP Server using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top