Advertisement here

Crud Android With Json And Mysql

ANDROID TUTORIAL CREATING CRUD WITH JASON AND MYSQL DATABASE



Welcome to . My previous post aboutThe most effective wayto speed up the latest IDM download, the nextarticle this time is a tutorialCreating Android CRUD AppswithJSONandMySQL. This app is made withEclipse SoftwareversionLunaand for the DataBase server useXamppplease those who don't have the software can browse on google.

Today is a presentation assignmentNetwork Programming Course, the reason is already tired of creating programsCRUD (Create, Update, Delete, Read)simple discourseClothing sales. However, because my group didn't present it in front of the class because there were certain arguments, rather than my useless project, I thought of just sharing it on the blog, maybe some people who have experienced the same task or are just looking forreference. (true story)

Android questions:

1. MakeAndroid Web Services Applicationcapable of processingCRUD( Add, Edit, Delete, Delete ) onMySQL Databasesby wearingJSON parsingorXML(I use JSON)

Provision :

1.DatabasesI useXAMPP
2.Web ServicealsoXAMPP
3.Android Clienti wearMy Android phoneto carry out the application

Material :

1. SoftwareLuna version Ecipseto create an Application
2. Web Server and DataBase useXAMPP
3.Android Phoneas Client

Tutorials:

1. CreateMySQL Databaseby the namefashionand tabledb_clothingwith the contents of the fieldid, brand, type, sizeandpricedata typevarcharexcept id data typei integer and primary



2. Create a PHP file that is used to replace the fileMySQL Databaseaccording to the client's request, please enter the folderC:xampphtdocsfashionand create a fileserver.phpin the file containsadd, edit, delete scripts, view data as usual

Mysql Connection Error :
" . mysql_error()); mysql_select_db($database) or die("

Database Connection Error:

" .
mysql_error());  @$operation = $_GET'operation';  switch ($operation) case "view": $query_show_clothes = mysql_query("SELECT * FROM db_clothes")
or die(mysql_error());  $data_array = array();  while ($data = mysql_fetch_assoc($query_show_clothes)) $data_array = $data;  echo json_encode($data_array);  breaks;  case "insert": /* Source code for Insert data */ @$brand = $_GET'brand';  @$type = $_GET'type';  @$size = $_GET'size';  @$price = $_GET'price';  $query_insert_data = mysql_query("INSERT INTO db_clothing
(brand, type, size, price) VALUES('$brand', '$type', '$size', '$price')"); if ($query_insert_data) echo "Data Saved Successfully"; else echo "Error  Insert Clothes " . mysql_error(); break; case "get_clothes_by_id": @$id = $_GET'id'; $query_apparel_apparel = mysql_query("SELECT * FROM db_clothes
WHERE id='$id'"
) or die(mysql_error());  $data_array = array();  $data_array = mysql_fetch_assoc($query_show_clothes);  echo "" .  json_encode($data_array) .  "";  breaks;  case "update": /* Source code for Update Biodata */ @$brand = $_GET'brand';  @$type = $_GET'type';  @$size = $_GET'size';  @$price = $_GET'price';  @$id = $_GET'id';  $query_update_clothing = mysql_query("UPDATE db_clothing SET brand='$brand',
type='$type' , size='$size',price='$price' WHERE id='$id'"); if ($query_update_clothing) echo "Data Update Successful"; else echo mysql_error(); break;  case "delete": /* Source code for Delete Biodata */ @$id = $_GET'id'; $query_delete_clothing = mysql_query("DELETE FROM db_clothing WHERE
 id='$id'");         if ($query_delete_pakaian)              echo "Delete Data Berhasil";          else              echo mysql_error();                   break;      default:         break;  ?>
3. Open SoftwareEclipsecreate a new project with the namefashion, then in chaptersrcmake 3 classes isMainActivity.java , Connections.Java , Clothing.javaand in theres - layoutthere is a performance file with the formatxml

 TUTORIAL ANDROID MEMBUAT CRUD DENGAN JASON DAN DATABASE MYSQL CRUD Android Dengan JSON dan MySQL

4. Previously openAndroidManifest.xml(the file is below itself)

 then add android:name="android.permission.INTERNET" />
 
to allow internet access to be
 
  xmlns:android="http://schemas.android.com/apk/res/android"     package="jhohannes.priba"     android:versionCode="1"     android:versionName="1.0" >               android:minSdkVersion="8"         android:targetSdkVersion="8" />       then add android:name="android.permission.INTERNET" />               android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >                      then add android:name="jhohannes.purba.MainActivity"             android:label="@string/app_name" >                               then add android:name="android.intent.action.MAIN" />                   then add android:name="android.intent.category.LAUNCHER" />                              
5. Openres - Layout - Activity_main.xmlfill in the following script

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.examplore.deathnote.MainActivity" >
   
   
android:id="@+id/buttonAddClothing"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
        android:text="Tambah Pakaian" />

   
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
        android:layout_height="wrap_content" >

       
android:id="@+id/verticalScrollView"
android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

           
android:id="@+id/tableClothes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
                android:layout_marginTop="20dp" >
           
       
   

In the layout I created the TableLayout was onVerticalScrollViewin the ScrollView, this works so that data that has a width of more than the screen size automaticallyscrollso that all data can be seen.

6. The following is thecontents of the script in

the Connection.java packagecom.examplore.deathnote;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Connection

public String call(String url)
int BUFFER_SIZE = 2000;
InputStream in = null;
try
in = OpenHttpConnection(url);
catch(IOException e)
e.printStackTrace();
returns "";
         
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char inputBuffer = new charBUFFER_SIZE;
try
           while ((charRead = isr.read(inputBuffer)) >0)
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new charBUFFER_SIZE;
          
in.close();
catch(IOException e)
// Handle Exception
e.printStackTrace();
returns "";
         
return str;
         

private InputStream OpenHttpConnection(String url) throws IOException
InputStream in = null;
int response = -1;
URL url1 = new URL(url);
URLConnection conn = url1.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not An Http Connection");
try
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpconn.setAllowUserInteraction(false);
httpconn.setInstanceFollowRedirects(true);
httpconn.setRequestMethod("GET");
httpconn.connect();
response = httpconn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
in = httpconn.getInputStream();
          
catch(Exception e)
throw new IOException("Error connecting2");
         
return in;
         
7. The following is the contents of the script onclothes.javafor the IP adjust tonetwork IPwhich is obtained for example if the computer IP192.168.43.94then also fill it in the IP project192.168.43.94

com.clothes.clothes;
public class Clothing extends Connection
String URL = "http://192.168.43.94/clothes/server.php";
String url = "";
String response = "";

public String showClothing()
try
url = URL + "?operation=view";
System.out.println("User Show URL: " + url);
response = call(url);
catch(Exception e)
     
return responses;
    

public String insertCloth(String brand, String type, String size, String price)
try
url = URL + "?operation=insert&brand=" + brand + "&type=" + type+ "&size=" + size+ "&price=" + price;
System.out.println("Insert Clothing URL : " + url);
response = call(url);
catch(Exception e)
     
return responses;
    

public String getClothesById(int id)
try
url = URL + "?operation=get_clothes_by_id&id=" + id;
System.out.println("Insert Clothing URL : " + url);
response = call(url);
catch(Exception e)
     
return responses;
    

public String updateCloth(String id, String brand, String type, String size, String price)
try
url = URL + "?operation=update&id=" + id + "&brand=" + brand + "&type=" + type + "&size=" + size + "&price=" + price;
System.out.println("Insert Biodata URL : " + url);
response = call(url);
catch(Exception e)
     
return responses;
    

public String deleteCloth(int id)
try
url = URL + "?operation=delete&id=" + id;
System.out.println("Insert Clothing URL : " + url);
response = call(url);
catch(Exception e)
     
return responses;
    

    
8. The following is the contents of the script onMainActivity.java package

the Connection.java packagecom.examplore.deathnote;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.examplore.deathnote.MainActivity;
import com.examplore.deathnote.Clothes;
import com.examplore.deathnote.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener

Outfit = new Outfit();
TableLayout tableClothes;

Button buttonAddClothing;
ArrayList
Data received fromBiodata.javastill in the form ofStringbut has structureJSON Encode. By entering the string into the JSONArray, the data will automatically be stored in the array. But keep in mind, that the data we receive from MySQL is data in the form of rows and columns

9. Done, please Copas later if typed, afraid of errors because it's too long hehe

Testing:

1. Turn onWifi / TheTeringIf there's no point, you have to have Wifi because later on, the laptop will be the place to store the database and Android as theClient must be connected with the same IP(eg laptop and android phone connected to campus wifi). For example laptop IP192.168.43.94put in projectclothes.java

2. Turn on Xampp Apache and MySQL just enough


3. Connectdata cableon an Android phone (provided that the driver has been installed) running a project to an Android phone, for exampleACER Z205

 TUTORIAL ANDROID MEMBUAT CRUD DENGAN JASON DAN DATABASE MYSQL CRUD Android Dengan JSON dan MySQL

4. Then see the picture below

 TUTORIAL ANDROID MEMBUAT CRUD DENGAN JASON DAN DATABASE MYSQL CRUD Android Dengan JSON dan MySQL
 TUTORIAL ANDROID MEMBUAT CRUD DENGAN JASON DAN DATABASE MYSQL CRUD Android Dengan JSON dan MySQL
 TUTORIAL ANDROID MEMBUAT CRUD DENGAN JASON DAN DATABASE MYSQL CRUD Android Dengan JSON dan MySQL

Download Project<< database sama koneksi buat sendiri mirip file diatas

That's the post aboutTutorial on creating Android CRUD Apps with JSONand MySQL to be useful, This is not a guide for newbies who are new to Android before learning about JSON Arrays, ordinary Android CRUD, XML etc. This is just my coursework that hasn't been presented yet. May be useful.


http://www.helmykediri.com

Previous Post
No Comment
Add Comment
comment url
Android