Sk Menu Restoran Digital Dengan Php Mysql - Android | Java ...

October 28, 2017 | Author: Anonymous | Category: PHP, Java, MySQL
Share Embed


Short Description

Membangun Web Service untuk menghubungkan Aplikasi PHP-MySQL dengan Aplikasi ANDROID dengan JSON. 3. Membangun Aplikasi ...

Description

Studi Kasus “MENU RESTORAN DIGITAL” dengan MySQL Server - ANDROID POKOK BAHASAN 1. 2. 3. 4.

Teknologi Client-Server AsyncTask Web Server dengan Parsing JSON. Menampilkan dengan Custom ListView

Skenario dan Rancangan Antarmuka Restoran / Rumah Makan yang biasanya menyajikan daftar Menu Konvensional diganti dengan daftar Menu Digital dan dapat di akses dengan Tablet atau HP dengan sangat mudah & praktis. Ada tiga tahapan / langkah kerja yang harus dilakukan, sbb: 1. Membangun Server Local Basisdata. 2. Membangun Web Service untuk menghubungkan Aplikasi PHP-MySQL dengan Aplikasi ANDROID dengan JSON. 3. Membangun Aplikasi Client untuk mengakses Server dengan ANDROID. Membangun Server Local Basisdata. Langkah-1: Buat database & tabelnya Create database MenuDigital Use MenuDigital Create table tb_kategori (idkategori int(2) not null AUTO_INCREMENT primary key,kategori varchar(15) not null) create table tb_menu (idmenu int(2) not null AUTO_INCREMENT primary key,nama_menu varchar(35) not null,harga_menu varchar(16) not null, diskripsi_menu text not null,pic_menu text not null,idkategori int(2) not null) Langkah-2: Membangun Web Service dengan JSON. 2.1 FILE: config.php 2.2.FILE:menu_service.php Langkah-3: Membangun Aplikasi ANDROID AplicationName : MenuDigital ProjectName : MenuDigital PackageName : com.menu.digital ========================================= Activity Name : MainActivity (.JAVA) Layout Name : main (.XML)

3.1. FILE: strings.xml (res-> values) MenuDigital Settings Hello world! 3.2. FILE: activity_main.xml (res-> layout)



3.3. FILE: activity_main_listview.xml (res-> layout) 3.4. FILE: dialog_custom.xml (res-> layout)



3.5. FILE: item_custom_listview.xml (res-> layout)



Langkah-4: Membuat Class JAVA 4.1 FILE:AmbilData.java (src->com.menu.digital) package com.menu.digital; import java.io.IOException; import java.util.ArrayList; import java.util.List; import import import import import

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

import import import import

android.app.ProgressDialog; android.content.Context; android.os.AsyncTask; android.util.Log;

public class AmbilData extends AsyncTask { private JSONParser jsonparser; ArrayList d; JsonObjectResult jobres; Context context; ProgressDialog pd; public void init(Context c, JsonObjectResult jres, String kategori, String url) { this.context = c; this.jobres = jres; AmbilData ad = this; ad.execute(url, kategori, ""); } @Override

protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); pd = ProgressDialog.show(context, "Retrieve Data", "aaa"); pd.setMessage("Please wait..."); pd.show(); } @Override protected Object doInBackground(Object... parameter) { // TODO Auto-generated method stub JSONObject jsobj = null; String url = (String) parameter[0]; String kat = (String) parameter[1]; Log.i("url", url); Log.i("kat", kat); jsonparser = new JSONParser(); List datajson = new ArrayList(); datajson.add(new BasicNameValuePair("kategori", kat)); try { jsobj = jsonparser.getObject(url, "GET", datajson); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsobj; } @Override protected void onPostExecute(Object result) { // TODO Auto-generated method stub super.onPostExecute(result); if (pd.isShowing()) { pd.dismiss(); } if (result != null) { JSONObject js = (JSONObject) result; jobres.gotJsonObject(js); } } public static abstract class JsonObjectResult { public abstract void gotJsonObject(JSONObject jobject); } }

4.2 FILE:JSONParser.java (src->com.menu.digital) package com.menu.digital; import java.io.BufferedReader;

import import import import import

java.io.IOException; java.io.InputStream; java.io.InputStreamReader; java.io.UnsupportedEncodingException; java.util.List;

import import import import import import import import import import import

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

public class JSONParser { InputStream is; String json; JSONObject object; public JSONObject getObject(String url, String method, List value) throws IOException { if(method == "POST"){ DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try { post.setEntity(new UrlEncodedFormEntity(value)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if(method == "GET"){ DefaultHttpClient client = new DefaultHttpClient(); String param = URLEncodedUtils.format(value, "utf8"); url += "?" + param; HttpGet get = new HttpGet(url);

HttpResponse response; try { response = client.execute(get); is = response.getEntity().getContent(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1")); StringBuilder sb = new StringBuilder(); String line; while((line = reader.readLine())!=null){ sb.append(line+"\n"); } is.close(); json = sb.toString(); object = null; } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { object = new JSONObject(json); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return object; } }

4.3 FILE:EntitasMakanan.java (src->com.menu.digital) package com.menu.digital; public class EntitasMakanan { int idmenu; String namamenu = ""; String hargamenu = ""; String deskripsimenu = ""; String picmenu = ""; public void setIDmenu(int id) { this.idmenu = id; }

public int getIDmenu() { return idmenu; } public void setNamaMenu(String n) { this.namamenu = n; } public String getNamaMenu() { return namamenu; } public void setHargaMenu(String h) { this.hargamenu = h; } public String getHargaMenu() { return hargamenu; } public void setDeskripsiMenu(String d) { this.deskripsimenu = d; } public String getDeskripsiMenu() { return deskripsimenu; } public void setPicMenu(String p) { this.picmenu = p; } public String getPicMenu() { return picmenu; } }

4.4 FILE:MainActivity.java (src->com.menu.digital) package com.menu.digital; import import import import import import

android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.app.Activity; android.content.Intent;

public class MainActivity extends Activity implements OnClickListener { Button bFood, bDrink; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); bFood = (Button) findViewById(R.id.bMakanan); bDrink = (Button) findViewById(R.id.bMinuman); bFood.setOnClickListener(this); bDrink.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.bMakanan: Bundle b = new Bundle(); Intent intent = new Intent(MainActivity.this, MenuActivity.class); b.putString("kategori", "1"); intent.putExtras(b); startActivity(intent); break; case R.id.bMinuman: Bundle bu = new Bundle(); Intent in = new Intent(MainActivity.this, MenuActivity.class); bu.putString("kategori", "2"); in.putExtras(bu); startActivity(in); break; } } }

4.5 FILE:MenuActivity.java (src->com.menu.digital) package com.menu.digital; import java.io.InputStream; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import import import import

com.menu.digital.AmbilData.JsonObjectResult; android.os.AsyncTask; android.os.Bundle; android.app.Activity;

import import import import import import import import import import import import import import

android.app.Dialog; android.graphics.Bitmap; android.graphics.BitmapFactory; android.util.Log; android.view.View; android.view.View.OnClickListener; android.view.ViewGroup.LayoutParams; android.view.WindowManager; android.widget.AdapterView; android.widget.Button; android.widget.ImageView; android.widget.TextView; android.widget.AdapterView.OnItemClickListener; android.widget.ListView;

public class MenuActivity extends Activity { EntitasMakanan entitasmakanan; ArrayList menu = new ArrayList(); ListView lv; String url = "http://10.0.2.2/menudigital/menu_services.php"; String urlpic = "http://10.0.2.2/menurestoran/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_listview); lv = (ListView) findViewById(R.id.listMenu); Bundle b = this.getIntent().getExtras(); if (b.containsKey("kategori")) { String kat = b.getString("kategori"); Log.d("kat", kat); AmbilData ambildata = new AmbilData(); ambildata.init(MenuActivity.this, jsresult, kat, url); } } public JsonObjectResult jsresult = new JsonObjectResult() { @Override public void gotJsonObject(JSONObject jobject) { // TODO Auto-generated method stub try { JSONArray arraytempat =

jobject.getJSONArray("datamenu"); for (int i = 0; i < arraytempat.length(); i++) { entitasmakanan = new EntitasMakanan(); entitasmakanan.setIDmenu(arraytempat.getJSONObject(i) .getInt("idmenu")); entitasmakanan.setNamaMenu(arraytempat.getJSONObject(i) .getString("nama_menu")); entitasmakanan.setHargaMenu(arraytempat.getJSONObject(i) .getString("harga_menu")); entitasmakanan.setDeskripsiMenu(arraytempat .getJSONObject(i).getString("diskripsi_menu")); entitasmakanan.setPicMenu(arraytempat.getJSONObject(i) .getString("pic_menu")); menu.add(entitasmakanan); } } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } MenuBaseAdapter datatempat = new MenuBaseAdapter(MenuActivity.this, menu); lv.setAdapter(datatempat); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int p, long arg3) { // TODO Auto-generated method stub String des = menu.get(p).getDeskripsiMenu(); String nm = menu.get(p).getNamaMenu(); String hrg = menu.get(p).getHargaMenu(); String pic = menu.get(p).getPicMenu();

tampilkandetail(nm, des, hrg, pic); } }); } }; public void tampilkandetail(String nama, String desk, String harga, String pic) { final Dialog d = new Dialog(this); d.setTitle("detail"); d.setContentView(R.layout.dialog_custom); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(d.getWindow().getAttributes()); lp.width = LayoutParams.MATCH_PARENT; lp.height = LayoutParams.WRAP_CONTENT; d.getWindow().setAttributes(lp); ImageView gb = (ImageView) d.findViewById(R.id.picDialog); TextView n = (TextView) d.findViewById(R.id.idNamaMenu); TextView h = (TextView) d.findViewById(R.id.idHarga); TextView desc = (TextView) d.findViewById(R.id.idDeskripsi); Button bOk = (Button) d.findViewById(R.id.idOK); n.setText(nama); h.setText(harga); desc.setText(desk); new DownloadImageTask(gb).execute(urlpic + pic); bOk.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub d.dismiss(); } }); d.show(); } public class DownloadImageTask extends AsyncTask { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { if (result != null) { Bitmap bmp2 = Bitmap.createScaledBitmap(result, 72, 72, true); bmImage.setImageBitmap(bmp2); } } } } 4.6 FILE:MenuBaseAdapter.java (src->com.menu.digital) package com.menu.digital; import java.io.InputStream; import java.util.ArrayList; import import import import import import

android.content.Context; android.graphics.Bitmap; android.graphics.BitmapFactory; android.os.AsyncTask; android.util.Log; android.view.LayoutInflater;

import import import import import

android.view.View; android.view.ViewGroup; android.widget.BaseAdapter; android.widget.ImageView; android.widget.TextView;

public class MenuBaseAdapter extends BaseAdapter { private static ArrayList searchArrayList; private LayoutInflater mInflater; String urlpic = "http://10.0.2.2/menurestoran/"; Bitmap bm; public MenuBaseAdapter(Context context, ArrayList results) { searchArrayList = results; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return searchArrayList.size(); } @Override public Object getItem(int p) { return searchArrayList.get(p); } @Override public long getItemId(int p) { return p; } @Override public View getView(int p, View v, ViewGroup parent) { ViewHolder holder; if (v == null) { v = mInflater.inflate(R.layout.item_custom_listview, null); holder = new ViewHolder(); holder.nama = (TextView) v.findViewById(R.id.nama); holder.harga = (TextView) v.findViewById(R.id.harga); holder.pic = (ImageView) v.findViewById(R.id.img_menu); // holder.deskripsi = (TextView)

v.findViewById(R.id.d); new DownloadImageTask(holder.pic).execute(urlpic + searchArrayList.get(p).getPicMenu()); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.nama.setText(searchArrayList.get(p).getNamaMenu()) ; holder.harga.setText(searchArrayList.get(p).getHargaMenu( )); return v; } static class ViewHolder { TextView nama, harga; ImageView pic; } public class DownloadImageTask extends AsyncTask { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11;

} protected void onPostExecute(Bitmap result) { if (result != null) { Bitmap bmp2 = Bitmap.createScaledBitmap(result, 72, 72, true); bmImage.setImageBitmap(bmp2); } } } }

4.7 FILE:ClientToServer.java (src->com.menu.digital) package com.menu.digital; import import import import

java.io.BufferedReader; java.io.InputStreamReader; java.net.URI; java.util.ArrayList;

import import import import import import import import import import

org.apache.http.HttpResponse; org.apache.http.NameValuePair; org.apache.http.client.HttpClient; org.apache.http.client.entity.UrlEncodedFormEntity; org.apache.http.client.methods.HttpGet; org.apache.http.client.methods.HttpPost; org.apache.http.conn.params.ConnManagerParams; org.apache.http.impl.client.DefaultHttpClient; org.apache.http.params.HttpConnectionParams; org.apache.http.params.HttpParams;

public class ClientToServer { public static final int HTTP_TIMEOUT = 30 * 1000; private static HttpClient client; private static HttpClient getHttpClient() { if (client == null) { client = new DefaultHttpClient(); final HttpParams parameterHttp = client.getParams(); HttpConnectionParams.setConnectionTimeout(parameterHttp,HTTP_T IMEOUT); ConnManagerParams.setTimeout(parameterHttp,HTTP_TIMEOUT); } return client; } public static String eksekusiHttpPost(String url, ArrayList postParameter) throws Exception { BufferedReader in = null;

try { HttpClient klien = getHttpClient(); HttpPost req = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( postParameter); req.setEntity(formEntity); HttpResponse jawaban = klien.execute(req); in = new BufferedReader(new InputStreamReader(jawaban.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String hasil = sb.toString(); return hasil; } finally { if (in != null) { in.close(); } } } public static String eksekusiHttpGet(String url) throws Exception { BufferedReader in = null; try { HttpClient hc = getHttpClient(); HttpGet req = new HttpGet(); req.setURI(new URI(url)); HttpResponse resp = hc.execute(req); in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String hasil = sb.toString(); return hasil; } finally { if (in != null) { in.close(); } } } }

View more...

Comments

Copyright © 2017 DATENPDF Inc.