Professional Documents
Culture Documents
http://hsoewarno.blogspot.com
androidCloud
By: Hendra Soewarno(0119067305)
1. Instalasi WAMPServer
2. Persiapan database MySQL pada WAMPServer
3. Persiapan Framework
library.php
Buka file library.php, dan pastikan koneksi ke database “androidCloud”, user dan password adalah
sudah benar.
<?php
/*
Jangan hapus/ubah bagian ini, untuk penghargaan kepada hak cipta
function openConnection() {
try {
$con = new PDO('mysql:host=localhost;port=3306;dbname=androidCloud', 'root', '');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
return $con;
}
//Select
function querySingleValue($con, $sSql, $values) {
//return single Value
if (!startsWith(strtolower($sSql),"select"))
throw new Exception("0: Invalid Select statement!");
try {
if (!($stmt = $con->prepare($sSql))) {
throw new Exception("0: (" . $con->errno . ") " . $con->error);
} else {
$paramValues = $values;
if (strpos($sSql, "?")===false) {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
$stmt->execute();
if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
return $row[0];
} else {
return null;
}
}
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
}
try {
if (!($stmt = $con->prepare($sSql))) {
throw new Exception("0: (" . $con->errno . ") " . $con->error);
} else {
$paramValues = $values;
if (strpos($sSql, "?")===false) {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam($paramCount, $paramValues[$i]);
}
}
$stmt->execute();
if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
return $row;
} else {
return null;
}
}
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
}
//CRUD
function createRow($con, $sSql, $values) {
//return row Affected
if (!startsWith(strtolower($sSql),"insert"))
throw new Exception("0: Invalid Insert statement!");
try {
if (!($stmt = $con->prepare($sSql))) {
throw new Exception("0: (" . $con->errno . ") " . $con->error);
} else {
$paramValues = $values;
if (strpos($sSql, "?")===false) {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam($paramCount, $paramValues[$i]);
}
}
$stmt->execute();
return $stmt->rowCount();
}
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
}
try {
if (!($stmt = $con->prepare($sSql))) {
throw new Exception("0: (" . $con->errno . ") " . $con->error);
} else {
$paramValues = $values;
if (strpos($sSql, "?")===false) {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam($paramCount, $paramValues[$i]);
}
}
$stmt->execute();
return $stmt->rowCount();
}
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
}
try {
if (!($stmt = $con->prepare($sSql))) {
throw new Exception("0: (" . $con->errno . ") " . $con->error);
} else {
$paramValues = $values;
if (strpos($sSql, "?")===false) {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
for ($i=0; $i<sizeof($values);$i++) {
$paramCount = $i+1;
$paramValues[$i] = $values[$i];
$stmt->bindParam($paramCount, $paramValues[$i]);
}
}
$stmt->execute();
return $stmt->rowCount();
}
} catch (PDOException $e) {
throw new Exception("0: " . $e->getMessage());
}
}
//unit test
/*
$con = openConnection();
if ($con)
echo "OK!";
*/
?>
<?php
/*
Jangan hapus/ubah bagian ini, untuk penghargaan kepada hak cipta
class ListGenerator {
public $con;
public $sSQL;
public $rowCount;
//
public function __construct($con, $sSQL) {
$this->con = $con;
$this->sSQL = $sSQL;
}
if (!($stmt = $this->con->prepare($this->sSQL))) {
throw new Exception("0:(" . $con->errno . ") " . $con->error);
} else {
$this->rowCount = 0;
if (strpos($this->sSQL, "?")===false) {
for ($i=0; $i<sizeof($paramValues);$i++) {
$paramCount = $i+1;
$stmt->bindParam(":" . $paramCount, $paramValues[$i]);
}
} else {
$stmt->execute();
if ($values = $stmt->fetch(PDO::FETCH_NUM)) {
do {
if ($this->onFilter($values)) {
$this->rowCount++;
$this->onDetail($values, $this->rowCount);
}
} while ($values = $stmt->fetch(PDO::FETCH_NUM));
}
$this->onSummary($this->rowCount);
}
$stmt->closeCursor();
} catch (PDOException $e) {
//$this->onError($values, $row, $e->getCode());
throw new Exception($e->getMessage(),$e->getCode());
}
}
}
?>
testSaveHTTP.php
<?php
include_once "framework/library.php";
$con = openConnection();
$nim = $_GET['nim'];
$nama = $_GET['nama'];
$nilai = $_GET['nilai'];
try {
if (createRow($con, "insert into siswa (nim, nama, nilai) values (?,?,?)",array($nim, $nama, $nilai)) >
0)
echo "sukses";
} catch (Exception $e) {
echo $e->getMessage();
}
?>
testGetDataHTTP.php
<?php
include_once "framework/library.php";
include_once "framework/listGenerator.php";
$con = openConnection();
$ssql = <<<EOD
SELECT nim, nama, nilai from siswa
EOD;
$list = new CreateJSON($con, $ssql);
echo "{data:[";
$list->generate(null);
echo "]}";
?>
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
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=".MainActivity" >
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:onClick="tambahDataOnClick"
android:text="Tambah Data" />
<Button
android:id="@+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonCamera"
android:layout_below="@+id/buttonCamera"
android:onClick="tampilDataOnClick"
android:text="Tampil Data" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
tambah_data.xml
<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=".MainActivity" >
<TextView
android:id="@+id/baris1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nim" />
<EditText
android:id="@+id/editNim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/baris2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama" />
<EditText
android:id="@+id/editNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nilai" />
<EditText
android:id="@+id/editNilai"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonSaveOnClick"
android:text="Save" />
</LinearLayout>
TambahData.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
setContentView(R.layout.tambah_data);
}
URL url;
try {
url = new URL("http://172.21.5.240/androidCloud/testSaveHTTP.php?nim=" +
nim
+ "&nama=" + nama + "&nilai=" + nilai);
});
saveThread.start();
try {
saveThread.join(); // wait until Thread finish
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tampilToast(result);
} else {
tampilToast("No network connection available.");
}
}
tampil_data.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
tampil_datadtl.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/baris1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp" />
<TextView
android:id="@+id/baris2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="14sp" />
</LinearLayout>
TampilData.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tampil_data);
refreshView();
}
item.put("nama", siswa.getString("nama"));
list.add(item);
}
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hendra.androidcloud.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
TambahDataAsyncTask.java
package com.hendra.androidcloud;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tambah_data);
}
URL url;
try {
url = new URL(
"http://172.21.5.240/androidCloud/testSaveHTTP.php?nim="
+ nim + "&nama=" + nama + "&nilai=" + nilai);
@Override
protected String doInBackground(String... data) {
return saveToServer(data[0], data[1], data[2]);
}
} else {
tampilToast("No network connection available.");
}
}
TampilDataAsyncTask.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tampil_data);
refreshView();
}
@Override
protected String doInBackground(Void... params) {
return getDataFromServer();
}
JSONObject jsonObj;
try {
jsonObj = new JSONObject(jsonStr);
JSONArray jArr = jsonObj.getJSONArray("data");