Tidak usah banyak cerita lagi akan saya tulis contoh code yang sudah saya buat yaitu algoritma untuk mengambil data siswa berdasarkan nomer NIS. Ini hanya contoh jadi make masalah yang sederhana saja, nahh.... kalau udah masuk ke soal baru saya akan bikin soal yang sulit, ckckckck kaya guru SMA saja kalau kasih contoh mudah tapi kalau udah soal sulit, Yaaa.... iya lah.... kan biar kelihatan pinter gurunya, padahal.....????? yaa memang pinter sih..... Oh ya catatan saja, saya hanya akan memberikan penggalan code saja, jadi bagi pemula jangan terlalu berharap banyak (sok.. banget sih... emangnya saya dah advance pa...!!!!), mending langsung tanya ke saya via email.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ResourceData;
/**
*
* @author Hendrawan
*/
public class Siswa {
private String NIS;
private String Name;
private String Address;
private Exception except = new Exception();
public String getNIS() {
return NIS;
}
public void setNIS(String NIS) {
this.NIS = NIS;
}
public String getAddress() {
return Address;
}
public void setAddress(String Address) {
this.Address = Address;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public Exception getExcept() {
return except;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Engine;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
*
* @author Hendrawan
*/
public class PostRequest {
static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
byte[] postBytes = null;
String url = null;
public PostRequest(String url, Hashtable params) throws Exception {
this.url = url;
String boundary = getBoundaryString();
String boundaryMessage = getBoundaryMessage(boundary, params);
String endBoundary = "\r\n--" + boundary + "--\r\n";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(boundaryMessage.getBytes());
bos.write(endBoundary.getBytes());
this.postBytes = bos.toByteArray();
bos.close();
}
String getBoundaryString() {
return BOUNDARY;
}
String getBoundaryMessage(String boundary, Hashtable params) {
StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
Enumeration keys = params.keys();
while(keys.hasMoreElements())
{
String key = (String)keys.nextElement();
String value = (String)params.get(key);
res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")
.append("\r\n").append(value).append("\r\n")
.append("--").append(boundary).append("\r\n");
}
return res.toString();
}
public InputStream sendStream() throws Exception {
HttpConnection hc = null;
DataInputStream dis = null;
try {
hc = (HttpConnection) Connector.open(url);
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
hc.setRequestMethod(HttpConnection.POST);
OutputStream dout = hc.openOutputStream();
dout.write(postBytes);
dout.close();
dis = new DataInputStream(hc.openDataInputStream());
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try {
if(hc != null)
hc.close();
}
catch(Exception e2) {
e2.printStackTrace();
}
}
// disini hal yang aneh tapi jalan, nilai pengembalian fungsi harusnya InputStream, tetapi
// saya masukan sebagai DataInputStream
return dis;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Engine;
import ResourceData.Siswa;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import org.kxml2.io.*;
import org.xmlpull.v1.XmlPullParser;
//import org.xmlpull.v1.*;
/**
*
* @author Hendrawan
*/
public class GetSiswaData {
private ResourceData.Siswa SiswaData = new ResourceData.Siswa();
private Engine.AccountSetting AccountSetting = new Engine.AccountSetting();
private Engine.AppSettings AppDefaultData = new Engine.AppSettings();
private KXmlParser parser;
private Exception except = new Exception();
private String ContentPostResponse;
private Engine.PostRequest MessagePostRequest;
//parser = new KXmlParser();
private String NIS = "";
// static data
private String URLPage = "http://localhost/siswaprofile.php";
public String getContentPostResponse() {
return ContentPostResponse;
}
public Exception getExcept() {
return except;
}
public Siswa getSiswaData() {
return SiswaData;
}
private boolean PostMessage() {
try {
Hashtable params = new Hashtable();
params.put("siswa", NIS);
MessagePostRequest = new PostRequest(URLPage, params);
ParseResponse(MessagePostRequest.sendStream());
} catch (Exception ex) {
except = new Exception(ex.getMessage().toString());
return false;
}
return true;
}
private boolean ParseResponse(InputStream aXmlInputStream) {
parser = new KXmlParser();
try {
parser.setInput(new InputStreamReader(aXmlInputStream));
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "response");
SiswaData.setBlog(parser.getAttributeValue(0));
while(parser.nextTag() == XmlPullParser.START_TAG) {
parser.require(XmlPullParser.START_TAG, null, "nis");
SiswaData.setNIS(parser.nextText());
parser.require(XmlPullParser.END_TAG, null, "nis");
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "nama");
SiswaData.setName(parser.nextText());
parser.require(XmlPullParser.END_TAG, null, "nama");
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "alamat");
SiswaData.setAddress(parser.nextText());
parser.require(XmlPullParser.END_TAG, null, "alamat");
}
parser.require(XmlPullParser.END_TAG, null, "response");
} catch (Exception ex) {
except = new Exception(ex.getMessage().toString());
return false;
}
return true;
}
public boolean GetSiswaData(String aNIS) {
NIS = aNIS;
if (! PostMessage()) {
return false;
}
return true;
}
}
require_once('../dbconnect.inc.php');
$nis = "";
if (isset($_POST['siswa']))
$nis = $_POST['siswa'];
$getSiswaDataQuery = "select NIS,Nama,Alamat from Siswa where NIS='" . $nis . "' limit 1";
$resultSiswaDataQuery = mysql_query($getSiswaDataQuery, $link);
$dataSiswaDataQuery = mysql_fetch_row($resultSiswaDataQuery);
print(""); ");
print("" . $dataSiswaDataQuery[0] . "
print("" . $dataSiswaDataQuery[1] . "
print("" . $dataSiswaDataQuery[2] . "
print("
?>
Nahhh untuk memanggil deretan baris java diatas sebenarnya tinggal panggil fungsi GetSiswaData dan masukan nis siswa sebagai argument dari fungsi tersebut. Nahhh untuk mengambil datanya langsung saja gunakan $GetSiswaData.getSiswaData.getName() dst.
semoga bermanfaat, jangan lupa sering-sering nge-click iklan adsense saya, supaya aplikasi Friends Card lebih cepat terselesaikan, dan bisa dinikmati tidak hanya oleh pemilik handphone symbian 3rd FP1 dan FP2.
Post a Comment