Baiklah pada praktikum kali saya telah membuat project
DataNilai. Sebelum membuat project pertama saya telah membuat databsenya.
Disini nama database datanilai.
Database datanilai. Seperti gambar dibawah ini :
Setelah membuat databasenya kita akan memasukkan script
datanilai.php. seperti berikut ini :
<?php
$nim=$_POST['nim'];
$nama=$_POST['nama'];
$semester=$_POST['semester'];
$matakuliah=$_POST['matakuliah'];
$nilai=$_POST['nilai'];
$conn =
mysql_connect("localhost","root","");
mysql_select_db("datanilai");
$query = "INSERT INTO
data_nilai (nim,nama,semester,matakuliah,nilai) values
('$nim','$nama','$semester','$matakuliah','$nilai')";
$result = mysql_query($query) or
die("REPORT...Gagal Query Simpan Data.");
if (mysql_num_rows($result) == 1){
echo 1;
}
else {
// print status message
echo 0;
}
?>
Seteleh itu kita akan membuat project DataNilai. Berikut ini
Class CustomHttpClient.java :
package com.datanilai;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import java.net.URI;
import
java.util.ArrayList;
import
org.apache.http.HttpResponse;
import
org.apache.http.NameValuePair;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.conn.params.ConnManagerParams;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.params.HttpConnectionParams;
import
org.apache.http.params.HttpParams;
public class
CustomHttpClient {
/** The time it takes for our client
to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000; //
milliseconds
/** Single instance of our HttpClient
*/
private static HttpClient mHttpClient;
/**
* Get our single instance of our
HttpClient object.
*
* @return an HttpClient
object with connection parameters set
*/
private static HttpClient
getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new
DefaultHttpClient();
final HttpParams
params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params,
HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params,
HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params,
HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the
specified url with the
*
specified parameters.
*
* @param url The web
address to post the request to
* @param
postParameters The parameters to send via the request
* @return The result of
the request
* @throws Exception
*/
public static String
executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new
UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response =
client.execute(request);
in = new BufferedReader(new
InputStreamReader(response.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 result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the
specified url.
*
* @param url The web
address to post the request to
* @return The result of
the request
* @throws Exception
*/
public static String
executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response =
client.execute(request);
in = new BufferedReader(new
InputStreamReader(response.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 result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
Kemudian Class Datanilai.java :
package com.datanilai;
import
java.util.ArrayList;
import
org.apache.http.NameValuePair;
import
org.apache.http.message.BasicNameValuePair;
import
com.datanilai.CustomHttpClient;
import
android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public class Datanilai extends Activity {
EditText
nim,nama,semester,matakuliah,nilai;
Button kirimdata,keluar;
TextView status;
/** Called when the activity is first
created. */
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nim=(EditText)findViewById(R.id.nim_editText);
nama=(EditText)findViewById(R.id.nama_editText);
semester=(EditText)findViewById(R.id.semester_editText);
matakuliah=(EditText)findViewById(R.id.matakuliah_editText);
nilai=(EditText)findViewById(R.id.nilai_editText);
kirimdata=(Button)findViewById(R.id.kirim_btn);
keluar=(Button)findViewById(R.id.keluar_btn);
status = (TextView)findViewById(R.id.status);
kirimdata.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v)
{
ArrayList<NameValuePair>
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("nim", nim.getText().toString()));
postParameters.add(new
BasicNameValuePair("nama", nama.getText().toString()));
postParameters.add(new
BasicNameValuePair("semester", semester.getText().toString()));
postParameters.add(new
BasicNameValuePair("matakuliah", matakuliah.getText().toString()));
postParameters.add(new
BasicNameValuePair("nilai", nilai.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/datanilai/datanilai.php",
postParameters);
String res =
response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
status.setText(res);
if (res.equals("1")) status.setText("Data
tidak Tersimpan Ke server");
else status.setText("Data
berhasil disimpan ke server");
}
catch (Exception e) {
nama.setText(e.toString());
}
}
});
}
}
Setelah kita membuat classnya kita akan membuat Layoutnya
atau tampilannya yaitu main.xml. berikut ini :
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/widget61"
android:layout_height="50px"
android:background="#000"
android:layout_x="0px"
android:layout_y="0px"
android:layout_width="match_parent">
<TextView android:id="@+id/widget62"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Formulir
Pendaftaran"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#fff"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</TextView>
</RelativeLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/widget30"
android:text="Nim">
</TextView>
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="400px"
android:textSize="18sp"
android:id="@+id/nim_editText">
</EditText>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/widget30"
android:text="Nama">
</TextView>
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="400px"
android:textSize="18sp"
android:id="@+id/nama_editText">
</EditText>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/widget30"
android:text="Semester">
</TextView>
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="400px"
android:textSize="18sp"
android:id="@+id/semester_editText">
</EditText>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/widget31"
android:text="Matakuliah">
</TextView>
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="400px"
android:textSize="18sp"
android:id="@+id/matakuliah_editText">
</EditText>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/widget32"
android:text="Nilai">
</TextView>
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="400px"
android:textSize="18sp"
android:id="@+id/nilai_editText">
</EditText>
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:text="Kirim
Data"
android:layout_height="wrap_content"
android:layout_width="200px"
android:id="@+id/kirim_btn">
</Button>
<Button android:text="Keluar"
android:layout_height="wrap_content"
android:layout_width="200px"
android:id="@+id/keluar_btn">
</Button>
</LinearLayout>
Hasil Tampilannya :
Setelah itu kita akan menjalankan hasilnya :
Kemudian kita Lihat Hasilnya
didatabasenya :
0 komentar:
Posting Komentar