Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Webservice mobile Nama Kelompok : Ragil Setiawan ( )

Presentasi serupa


Presentasi berjudul: "Webservice mobile Nama Kelompok : Ragil Setiawan ( )"— Transcript presentasi:

1 Webservice mobile Nama Kelompok : Ragil Setiawan (15.11.0100)
Muhammad Ma’ruf ( ) Siti Rodhotul Jannah ( ) Tika Sari ( ) Nur Maya Bella ( ) Ferdatama ( )

2 Webservice mobile Mysql merupakan implementasi dari sistem manajemen basis data relasional atau disebut RDBMS (relational database management system) yang mendukung adanya relationship atau hubungan antar tabel. . Mysql digratiskan dibawah lisensi GPL (General Public License). MySQL merupakan  sebuah sistem yang berfungsi untuk menyimpan data (database). Intinya database adalah tempat yang dapat kita gunakan untuk “menyimpan” data yang dibutuhkan web kita.

3 PHP PHP Hypertext Preprocessor atau sering disebut PHP merupakan bahasa pemrograman berbasis server-side yang dapat melakukan parsing script PHP menjadi script web sehingga dari sisi client menghasilkan suatu tampilan yang menarik WEB SERVER Pada dasarnya fungsi utama web server adalah untuk melakukan transfer berkas yang di request oleh user melalui protocol komunikasi yang sudah ditentukan, request-request user dapat berupa berkas teks, video, gambar dan lainnya. Dengan adanya web server seluruh pemberkasan dalam bentuk multimedia atau teks, dan banyak lagi.

4 Buat Webservice Buat file muat_data.php <?php require_once'db_config.php'; $query="SELECT * FROM data_mahasiswa"; $sql=mysqli_query($con,$query); $ray=array(); while ($row = mysqli_fetch_array($sql)) { array_push($ray, array( "id"=> $row['id'], "nama"=> $row['nama'], "kelas"=> $row['kelas'] )); } echo json_encode($ray); mysqli_close($con); ?> Buat file db_config.php define('DB_USER', "root"); // db user define('DB_PASSWORD', ""); // db password (mention your db password here) define('DB_DATABASE', "mahasiswa"); // database name define('DB_SERVER', "localhost"); // db server $con=mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB _DATABASE); ?>

5 Buat Database

6 Buat file single_data. php <
Buat file single_data.php <?php $id=$_GET["id"]; require_once'db_config.php'; $query="SELECT * FROM data_mahasiswa where id='$id'"; $sql=mysqli_query($con,$query); $ray=array(); while ($row = mysqli_fetch_array($sql)) { array_push($ray, array( "id"=> $row['id'], "nama"=> $row['nama'], "kelas"=> $row['kelas'] )); } echo json_encode($ray); mysqli_close($con); ?> Buat file tambah_data.php <?php $nama_mhs=$_POST['nama']; $kelas_mhs=$_POST['kelas']; require_once('db_config.php'); $sql="INSERT INTO data_mahasiswa (nama,kelas) VALUES ('$nama_mhs','$kelas_mhs');"; mysqli_query($con,$sql); ?>

7 Buat file hapus_data. php <
Buat file hapus_data.php <?php $id_mhs=$_POST['id_mhs']; require_once('db_config.php'); $sql="DELETE FROM data_mahasiswa WHERE id=$id_mhs;"; mysqli_query($con,$sql); ?> Buat file edit_data.php <?php $id_mhs=$_POST['id']; $nama_mhs=$_POST['nama']; $kelas_mhs=$_POST['kelas']; require_once('db_config.php'); $sql="UPDATE data_mahasiswa SET nama='$nama_mhs',kelas='$kelas_mh s' WHERE id=$id_mhs;"; mysqli_query($con,$sql); ?>

8 Tutorial Membuat Aplikasi Android CRUD WebserviceDengan Database Mysql
Membuat ApiService.java sebagai service untuk mendapatkan data dari database package com.example.ragil.mahasiswa.API; import com.example.ragil.mahasiswa.Model.ModelData; import java.util.List; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query; /** * Created by JhonDev on 05/10/ */ } public interface ApiService { @FormUrlEncoded @POST("tambah_data.php") Call<ResponseBody> String String kelas); @FormUrlEncoded @POST("edit_data.php") Call<ResponseBody> String String String kelas); @FormUrlEncoded @POST("hapus_data.php") Call<ResponseBody> String id_mhs); @GET("muat_data.php") Call<List<ModelData>> getSemuaMhs(); @GET("single_data.php") Call<List<ModelData>> String id);

9 Membuat Class ModelData.java
package com.example.ragil.mahasiswa.Model; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class ModelData { @SerializedName("id") @Expose private String idMahasiswa; @SerializedName("nama") @Expose private String nama; @SerializedName("kelas") @Expose private String kelas_mhs; public static final String id_mahasiswa = "ID_MAHASISWA"; public static final String nama_mahasiswa = "ID_MAHASISWA"; public static final String jenis_mahasiswa = "ID_MAHASISWA"; public ModelData(String id, String nama, String kelas_mhs) { this.idMahasiswa = id; this.nama = nama; this.kelas_mhs = kelas_mhs; } public String getidMahasiswa() { return idMahasiswa; } public void setidMahasiswa(String idMahasiswa) { this.idMahasiswa = idMahasiswa; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } public String getKelas_mhs() { return kelas_mhs; } public void setKelas_mhs(String kelas_mhs) { this.kelas_mhs = kelas_mhs; } }

10 Membuat Class ListArray.java
private String kelas_mhs; public static final String id_mahasiswa = "ID_MAHASISWA"; public static final String nama_mahasiswa = "ID_MAHASISWA"; public static final String jenis_mahasiswa = "ID_MAHASISWA"; public ModelData(String id, String nama, String kelas_mhs) { this.idMahasiswa = id; this.nama = nama; this.kelas_mhs = kelas_mhs; } public String getidMahasiswa() { return idMahasiswa; } public void setidMahasiswa(String idMahasiswa) { this.idMahasiswa = idMahasiswa; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } public String getKelas_mhs() { return kelas_mhs; } public void setKelas_mhs(String kelas_mhs) { this.kelas_mhs = kelas_mhs; } }

11 Membuat Class DaftarMahasiswa.java untuk menampilkan data
package com.example.ragil.mahasiswa; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.example.ragil.mahasiswa.API.ApiService; import com.example.ragil.mahasiswa.Adapter.ListArrayAdapter; import com.example.ragil.mahasiswa.Model.ModelData; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class DaftarMahasiswa extends AppCompatActivity implements AdapterView.OnItemClickListener { ArrayList<ModelData> datamahasiswa = new ArrayList<ModelData>(); ListView listview; ListArrayAdapter adapter; LinearLayout layout_loading; TextView text_load; ImageView icon_load; Button btntambah; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.daftar_mahasiswa); layout_loading = (LinearLayout) findViewById(R.id.layout_loading); text_load = (TextView) findViewById(R.id.text_load); icon_load = (ImageView) findViewById(R.id.icon_load); listview = (ListView) findViewById(R.id.listMhsMhs); listview.setOnItemClickListener(DaftarMahasiswa.this); listview.setDividerHeight(0); setup(); btntambah=(Button) findViewById(R.id.tambah); btntambah.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent tambah= new Intent(DaftarMahasiswa.this,MainAdd.class); startActivity(tambah); } }); }

12 public void setup() { Retrofit retrofit = new Retrofit. Builder()
public void setup() { Retrofit retrofit = new Retrofit.Builder() baseUrl(MainActivity.ROOT_URL) addConverterFactory(GsonConverterFactory.create()) build(); ApiService service = retrofit.create(ApiService.class); Call<List<ModelData>> call = service.getSemuaMhs(); call.enqueue(new Callback<List<ModelData>>() { @Override public void onResponse(Call<List<ModelData>> call, Response<List<ModelData>> response) { datamahasiswa.clear(); if (response.isSuccessful()) { int jumlah = response.body().size(); for (int i = 0; i < jumlah; i++) { ModelData data = new ModelData( response.body().get(i).getidMahasiswa(), response.body().get(i).getNama(), response.body().get(i).getKelas_mhs()); datamahasiswa.add(data); Log.d("RESPON", "onResponse: " + response.body().get(i).getidMahasiswa()); } listview.setVisibility(View.VISIBLE); adapter = new ListArrayAdapter(DaftarMahasiswa.this, R.layout.row_mahasiswa, datamahasiswa); listview.setAdapter(adapter); if (adapter.getCount() < 1 ) { layout_loading.setVisibility(View.VISIBLE); String error = "Daftar mahasiswa Kosong"; text_load.setText(error); Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_data_kosong); icon_load.setImageBitmap(icon); } else { layout_loading.setVisibility(View.GONE); } } else { String error = "Error Retrive Data from Server !!!"; text_load.setText(error); Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_network); icon_load.setImageBitmap(icon); } }

13 @Override public void onFailure(Call<List<ModelData>> call, Throwable t) { String error = "Error Retrive Data from Server wwaau!!!\n" + t.getMessage(); text_load.setText(error); Bitmap icon = BitmapFactory.decodeResource(getResourc es(), R.drawable.ic_network); icon_load.setImageBitmap(icon); } }); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView ids = (TextView) view.findViewById(R.id.listID1); Intent intent = new Intent(DaftarMahasiswa.this, MainEdit.class); intent.putExtra(ModelData.id_mahasiswa, ids.getText().toString()); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { adapter.clear(); setup(); } } } 

14 Referensi server-local.html mysql.html php.html DpLheRYh8FCJkwK-d73FgKL&index=1

15 Membuat MainActivity.java
package com.example.ragil.mahasiswa; import android.os.Bundle; import android.support.v7.app.AppCompatAc tivity; public class MainActivity extends AppCompatActivity { public static final String ROOT_URL = " @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }

16 Membuat xml daftar_mahasiswa.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" /apk/res/android" xmlns:tools=" ols" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ragil.mahasisw a.DaftarMahasiswa"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:clipToPadding="false" android:padding="6dp" android:scrollbars="none" android:visibility="gone" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginBottom="35dp" android:layout_marginEnd="24dp" android:layout_marginRight="24dp" android:text="Button" /> <include android:layout_marginLeft="25dp" android:layout_marginStart="25dp" /> </RelativeLayout>

17 Membuat xml loading_and_result
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" /apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download Data From Server\nPlease wait..." android:textColor="#ffffff" android:textSize="15sp" android:textStyle="bold" android:gravity="center"/> </LinearLayout> </RelativeLayout>

18 Membuat xml row_mahasiswa.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" d" xmlns:app=" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="1dp" app:cardCornerRadius="7dp" android:layout_margin="3dp"> <LinearLayout android:background="#000" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="7dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nama Barang" android:textColor="#fff" android:textSize="17sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="Keterangan" android:textColor="#fff" android:textSize="12sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="right" android:text="Jenis Barang" android:textColor="#fff" android:textSize="12sp" android:textStyle="italic|bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>

19 Edit AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" es/android" package="com.example.ragil.mahasiswa"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:supportsRtl="true" <activity android:name=".DaftarMahasiswa"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainEdit"/> <activity android:name=".MainAdd"/> </application> </manifest>

20 Membuat class MainAdd.java
package com.example.ragil.mahasiswa; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.ragil.mahasiswa.API.ApiService; import com.example.ragil.mahasiswa.Model.ModelData; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; public class MainAdd extends AppCompatActivity { String ID_MAHASISWA; EditText et_id, et_nama, et_kelas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); ID_MAHASISWA = getIntent().getStringExtra(ModelData.id_mahasiswa); et_id = (EditText) findViewById(R.id.edit_id); et_nama = (EditText) findViewById(R.id.edit_nama); et_kelas = (EditText) findViewById(R.id.edit_kelas); final EditText edtNama = (EditText) findViewById(R.id.edit_nama); final EditText edtKelas = (EditText) findViewById(R.id.edit_kelas); Button btnAdd = (Button) findViewById(R.id.simpan); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sNama = String.valueOf(edtNama.getText()); String sKelas = String.valueOf(edtKelas.getText()); tambahData(sNama, sKelas); } }); }

21 public void tambahData(String nama, String kelas) { Retrofit retrofit = new Retrofit.Builder() baseUrl(MainActivity.ROOT_URL) addConverterFactory(new StringConverter()) build(); ApiService service = retrofit.create(ApiService.class); Call<ResponseBody> call = service.tambahData(nama, kelas); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { BufferedReader reader = null; String respon = ""; try { reader = new BufferedReader(new InputStreamReader(response.body().byteStream())); respon = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(MainAdd.this, respon, Toast.LENGTH_SHORT).show(); finish(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(MainAdd.this, t.getMessage(), Toast.LENGTH_SHORT).show(); } }); } }

22 Membuat xml activity_add.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" k/res/android" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:ems="10" android:inputType="textPersonName" android:hint="Nama Mahasiswa" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:ems="10" android:inputType="textPersonName" android:hint="Kelas Mahasiswa" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Simpan" /> </RelativeLayout>

23 Membuat Class MainEdit.java
package com.example.ragil.mahasiswa; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.ragil.mahasiswa.API.ApiService; import com.example.ragil.mahasiswa.Model.ModelData; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainEdit extends AppCompatActivity { String ID_MAHASISWA; EditText et_id, et_nama, et_kelas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_form); ID_MAHASISWA = getIntent().getStringExtra(ModelData.id_mahasiswa); et_id = (EditText) findViewById(R.id.edit_id); et_nama = (EditText) findViewById(R.id.edit_nama); et_kelas = (EditText) findViewById(R.id.edit_kelas); bindData(); Button btnUbah = (Button) findViewById(R.id.ubah); btnUbah.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sId = String.valueOf(et_id.getText()); String sNama = String.valueOf(et_nama.getText()); String sKelas = String.valueOf(et_kelas.getText()); editData(sId, sNama, sKelas); } });

24 Button btnDel = (Button) findViewById(R. id. simpan); btnDel
Button btnDel = (Button) findViewById(R.id.simpan); btnDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hapusData(ID_MAHASISWA); } }); // ditambahkan Button btnBatal = (Button) findViewById(R.id.batal); btnBatal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } public void bindData() { Retrofit retrofit = new Retrofit.Builder() baseUrl(MainActivity.ROOT_URL) addConverterFactory(GsonConverterFactory.create()) build(); ApiService service = retrofit.create(ApiService.class ); Call<List<ModelData>> call=service.getSingleData(ID_MAHASISWA); call.enqueue(new Callback<List<ModelData>>() { @Override public void onResponse(Call<List<ModelData>> call, Response<List<ModelData>> response) { if (response.isSuccessful()) { for (int i = 0; i < response.body().size(); i++) { et_id.setText(response.body().get(i).getidMahasiswa()); et_nama.setText(response.body().get(i).getNama()); et_kelas.setText(response.body().get(i).getKelas_mhs()); } } } @Override public void onFailure(Call<List<ModelData>> call, Throwable t) { } }); }

25 public void editData(String id, String nama, String kelas) { Retrofit retrofit = new Retrofit.Builder() baseUrl(MainActivity.ROOT_URL) addConverterFactory(new StringConverter()) build(); ApiService service = retrofit.create(ApiService.class); Call<ResponseBody> call = service.editData(id, nama, kelas); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { BufferedReader reader = null; String respon = ""; try { reader = new BufferedReader(new InputStreamReader(response.body().byteStream())); respon = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(MainEdit.this, respon, Toast.LENGTH_SHORT).show(); finish(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } }); } public void hapusData(String id) { Retrofit retrofit = new Retrofit.Builder() baseUrl(MainActivity.ROOT_URL) addConverterFactory(new StringConverter()) build(); ApiService service = retrofit.create(ApiService.class); Call<ResponseBody> call = service.hapusData(id); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { BufferedReader reader = null; String respon = ""; try { reader = new BufferedReader(new InputStreamReader(response.body().byteStream())); respon = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(MainEdit.this, respon, Toast.LENGTH_SHORT).show(); finish(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } });

26 Membuat xml main_edit.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:layout_marginTop="20dp" android:ems="10" android:inputType="textPersonName" android:hint="ID Mahasiswa" android:enabled="false"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:ems="10" android:inputType="textPersonName" android:hint="Nama Mahasiswa" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:ems="10" android:inputType="textPersonName" android:hint="Kelas Mahasiswa" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:layout_marginStart="18dp" android:layout_marginTop="34dp" android:text="UBAH" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Hapus" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginEnd="22dp" android:layout_marginRight="22dp" android:text="Batal" /> </RelativeLayout>

27 Referensi server-local.html mysql.html php.html DpLheRYh8FCJkwK-d73FgKL&index=1


Download ppt "Webservice mobile Nama Kelompok : Ragil Setiawan ( )"

Presentasi serupa


Iklan oleh Google