Loading...

How to Connect Android to A Remote Server Using JSON, MySQL and PHP

View: 479    Dowload: 1   Comment: 0   Post by: daii   Category: Mobile applications   Fields: Game - Entertainment

Download  how-to-connect-android-to-a.zip (7.01 MB)

You need to Sign In to download the file how-to-connect-android-to-a.zip
If you do not have an account then Sign up for free here

Android-MySQL connectivity using JSON and PHP.
INTRODUCTION 
To connect android app to a remote server, we use a technology called Volley. Before we start the coding, let’s see some basic theory behind the connection process.
CONCEPT

First the android phone creates a request to the server for asking data. The request contains an address of PHP file. The task of the PHP file is to communicate with the DB and create the list of values in JSON format. There is another advantage of using PHP file that is it gives back-end independence. Back-end independence is nothing but, no matter what front-end coding is used (Android, .NET, Objective C etc…), we no need to change the structure of back-end.
The server then creates a response, packs the JSON values in it and passes it back to the android phone. Normally JSON values will be in form of JSON Array. This is because the values might contain more number of rows, so each row is packed in the array.
STEPS TO BE FOLLOWED
There are four simple steps to be followed to connect MySQL DB to Android. They are
1) Create Request using RequestQueue
2) Create JSONArrayRequest
3) Get JSONObject from JSONArray
4) Add JSONArrayRequest to the request queue

EXPLAINATION OF THE STEPS
1) Create request using RequestQueue
• Before starting the communication, the front-end (android) should create a connection to the back-end. This connection is created using RequestQueue.
• The RequestQueue contains the list of request given by a client, and sends one by one to the server.
2) Create JSONArrayRequest
• Once the RequestQueue sends a request to the server, the server starts to process the request and communicates with the DB and created a list of JSON values.
• These list of values are packed inside one JSONArray.
• The JSONArray is sent to the client.
• The client (Android) receives the JSONArray using JSONArrayRequest.
3) Get JSONObject from JSONArray
• The JSONArray received from the server contains a list of JSONObject.
• In order to process each JSONObject, we have to convert the single JSONArray into list of JSON Objects.
• Once the list of JSON Object is obtained, then it is easy to process them.
4) Add JSOnArrayRequest to RequestQueue.
• The last and the most important step is to add the JSONArrayRequest into RequestQueue.
• If this step is not done, then the Request will never be sent to the server.

CODING
1) Create JSONArrayRequest


 
  1. RequestQueue r; //this line creates a RequestQueue Object.
  2. r= Volley.newRequestQueue(this); //initialize the RequestQueue.

2) Create JSONArrayRequest


 
  1. String url="<a href="http://www.vjenterprisevlr.com/phpfiles/readbooks.php";
  2. JsonArrayRequest" rel="nofollow">http://www.vjenterprisevlr.com/phpfiles/readbooks.php";
  3. JsonArrayRequest</a> jo=new JsonArrayRequest(Request.Method.POST, url, new Response.Listener<JSONArray>() { //this line creates a JSONArrayRequest, it passes the url of the PHP file, and specifies that the passing type is POST.
  4. @Override
  5. public void onResponse(JSONArray response) {
  6. }
  7. }
  8. }, new Response.ErrorListener() {
  9. @Override
  10. public void onErrorResponse(VolleyError error) {
  11.  
  12. }
  13. }
  14. );

3) Get JSONObject from JSONArray


 
  1. JSONArray books=response;
  2. for(int i=0;i<books.length();i++) //create list of JSONObjects from the JSONArray
  3. {
  4. try {
  5. JSONObject book = books.getJSONObject(i);
  6. String bookname=book.getString("BookName"); // get the bookname from JSONObject
  7. String price=book.getString("Price"); // get price from JSONObject
  8. txt.append(bookname+ "costs " + price+"\n"); //update output
  9. }
  10. catch(Exception e) //handle any exceptions
  11. {
  12.  
  13. }

4) Add JSONArrayRequest to RequestQueue


 
  1. r.add(jo); //add the request into the queue.

SOURCE CODE


 
  1. StartPage.java
  2. package tutorials.microfusionsmartsolutions.com.volleytest;
  3.  
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8.  
  9. import com.android.volley.Request;
  10. import com.android.volley.RequestQueue;
  11. import com.android.volley.Response;
  12. import com.android.volley.VolleyError;
  13. import com.android.volley.toolbox.JsonArrayRequest;
  14. import com.android.volley.toolbox.Volley;
  15.  
  16. import org.json.JSONArray;
  17. import org.json.JSONObject;
  18.  
  19. public class StartPage extends AppCompatActivity {
  20.  
  21. Button but;
  22. TextView txt;
  23. RequestQueue r;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_start_page);
  29. but=(Button) findViewById(R.id.button);
  30. txt=(TextView) findViewById(R.id.textView);
  31. String url="<a href="http://www.vjenterprisevlr.com/phpfiles/readbooks.php";
  32.  
  33. " rel="nofollow">http://www.vjenterprisevlr.com/phpfiles/readbooks.php";
  34.  
  35. </a> r= Volley.newRequestQueue(this);
  36.  
  37. JsonArrayRequest jo=new JsonArrayRequest(Request.Method.POST, url, new Response.Listener<JSONArray>() {
  38. @Override
  39. public void onResponse(JSONArray response) {
  40. JSONArray books=response;
  41. for(int i=0;i<books.length();i++)
  42. {
  43. try {
  44. JSONObject book = books.getJSONObject(i);
  45. String bookname=book.getString("BookName");
  46. String price=book.getString("Price");
  47. txt.append(bookname+ "costs " + price+"\n");
  48. }
  49. catch(Exception e)
  50. {
  51.  
  52. }
  53.  
  54. }
  55. }
  56. }, new Response.ErrorListener() {
  57. @Override
  58. public void onErrorResponse(VolleyError error) {
  59.  
  60. }
  61. }
  62. );
  63. r.add(jo);
  64. }
  65. }

XML Layout


 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  5. " rel="nofollow">http://schemas.android.com/tools"
  6. </a> android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context="tutorials.microfusionsmartsolutions.com.volleytest.StartPage">
  13.  
  14.  
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="GET"
  19. android:id="@+id/button"
  20. android:layout_alignParentTop="true"
  21. android:layout_centerHorizontal="true" />
  22.  
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text=""
  27. android:id="@+id/textView"
  28. android:layout_below="@+id/button"
  29. android:layout_marginTop="10dp"
  30. android:textSize="25dp"
  31. android:layout_alignParentEnd="true"
  32. android:layout_alignParentBottom="true"
  33. android:layout_alignParentStart="true" />
  34. </RelativeLayout>

How to Connect Android to A Remote Server Using JSON, MySQL and PHP

Android-MySQL connectivity using JSON and PHP. INTRODUCTION To connect android app to a remote server, we use a technology called Volley. Before we start the coding, let’s see some basic theory behind the connection process. CONCEPT First the android phone creates a request to the server for asking data. The request contains an address of PHP file. The task of the PHP file is to communicate with...

Posted on 05-04-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Android - Simple Image Capture

    Android - Simple Image Capture

    View: 589    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Image Capture using Android. This simple application can be used to any system that needed the information about the location of the user. The android is an open source operating system it's free and user friendly to...

  • Android - Simple Video Player

    Android - Simple Video Player

    View: 441    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Video Player using Android. This simple application can be use in viewing some videos. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any...

  • An Experience about Using Webview in Android Studio 3.0

    An Experience about Using Webview in Android Studio 3.0

    View: 221    Download: 0   Comment: 0

    Category: Android     Fields: Other

    I am blogging on ngocminhtran.com (using WordPress platform). Like most websites, I want to write an Android application to help viewers have a lot of great experiences with my blog.

  • Android - Simple Sending SMS

    Android - Simple Sending SMS

    View: 502    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Sending SMS using Android. This tutorial is only an example for sending an SMS, it does not contain the receiving message yet. Android is a mobile operating system developed by Google. It used in several gadget like...

  • Bomb Explode! (An Android App Strategy Game)

    Bomb Explode! (An Android App Strategy Game)

    View: 453    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This android app named Bomb Explode! is a capstone project that I made for a particular school. BOMB EXPLODE! is a Human Versus Computer strategy game. The objective is to eliminate the opponent by blowing up their BOMBS. Each player takes a turn by...

  • GIS App for Flood-Prone and Disease Affected Areas

    GIS App for Flood-Prone and Disease Affected Areas

    View: 451    Download: 1   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    GIS App for Flood-Prone and Disease Affected Areas is one of the capstone project that I have created in a particular school. Features of the application: - Geographic Map - GIS - Flood Prone Map - Urban Map - Search a particular area - Has SQLite...

  • NSO Demographic Application

    NSO Demographic Application

    View: 492    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This app is entitled NSO Demographic Application that was one of the capstone project/thesis project that I made for a particular school. This app features to provide a demographic survey of the National Statistics Office that will send to the...

  • HandEBook: An IT Portable Book Application

    HandEBook: An IT Portable Book Application

    View: 464    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This is an android mobile application entitled HandEBook: An IT Portable Book Application was one one of the capstone project that I have made in our clients at Cotabato. We used the Basic4Android software for the frontend and SQLite for the database...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd