berikut ini nama-nama class :
- CostumHttpClient.java
- Daftar_akta.java
- Daftar_user.java
- login.java
- menulayanan.java
Nama-nama layaout :
- daftar_akta.xml
- daftar_user.xml
- login.xml
- main.xml
sekarang kita akan membuat class CostumHttpClien.java:
package com.wilis;
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();
}
}
}
}
}
setelah kita akan membuat class daftar_akta.java :
package com.wilis;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class daftar_akta extends Activity {
EditText nomor_reg,nama,ttl,jam,anak_ke,ayah_nama,ayah_umur,ayah_pekerjaan,ayah_alamat,ibu_nama,ibu_umur,ibu_pekerjaan,ibu_alamat;
RadioGroup jk;
TextView status;
Button simpan,keluar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_akta);
nomor_reg = (EditText)findViewById(R.id.txtnomor_reg);
nama=(EditText)findViewById(R.id.txtnama);
ttl=(EditText)findViewById(R.id.txtttl);
jam=(EditText)findViewById(R.id.txtjam);
jk=(RadioGroup) findViewById(R.id.jekel);
anak_ke = (EditText) findViewById (R.id.txtanak_ke);
ayah_nama = (EditText)findViewById(R.id.txtayah_nama);
ayah_umur = (EditText)findViewById(R.id.txtayah_umur);
ayah_pekerjaan = (EditText)findViewById(R.id.txtayah_pekerjaan);
ayah_alamat = (EditText)findViewById(R.id.txtayah_alamat);
ibu_nama = (EditText)findViewById(R.id.txtibu_nama);
ibu_umur = (EditText)findViewById(R.id.txtibu_umur);
ibu_pekerjaan = (EditText)findViewById(R.id.txtibu_pekerjaan);
ibu_alamat = (EditText)findViewById(R.id.txtibu_alamat);
simpan=(Button)findViewById(R.id.btnsimpan);
keluar=(Button)findViewById(R.id.btnexit);
status=(TextView)findViewById(R.id.txtstatus);
simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//atur variabel utk menampung pilihan jenis kelamin
String type=null;
switch (jk.getCheckedRadioButtonId()) {
case R.id.pria:
type="Pria";
break;
case R.id.perempuan:
type="Perempuan";
break;
}
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("nomor_reg", nomor_reg.getText().toString()));
postParameters.add(new BasicNameValuePair("nama", nama.getText().toString()));
postParameters.add(new BasicNameValuePair("ttl",ttl.getText().toString()));
postParameters.add(new BasicNameValuePair("jam",ttl.getText().toString()));
postParameters.add(new BasicNameValuePair("jekel", type));
postParameters.add(new BasicNameValuePair("anak_ke", anak_ke.getText().toString()));
postParameters.add(new BasicNameValuePair("ayah_nama", ayah_nama.getText().toString()));
postParameters.add(new BasicNameValuePair("ayah_umur", ayah_umur.getText().toString()));
postParameters.add(new BasicNameValuePair("ayah_pekerjaan", ayah_pekerjaan.getText().toString()));
postParameters.add(new BasicNameValuePair("ayah_alamat", ayah_alamat.getText().toString()));
postParameters.add(new BasicNameValuePair("ibu_nama", ibu_nama.getText().toString()));
postParameters.add(new BasicNameValuePair("ibu_umur", ibu_umur.getText().toString()));
postParameters.add(new BasicNameValuePair("ibu_pekerjaan", ibu_pekerjaan.getText().toString()));
postParameters.add(new BasicNameValuePair("ibu_alamat", ibu_alamat.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/akta/daftar_akta.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) {
nomor_reg.setText(e.toString());
}
}
});
}
public void keluar (View theButton)
{
Intent a = new Intent (this,menulayanan.class);
startActivity(a);
}
}
kemudian kita akan membuat class daftar_user.java :
package com.wilis;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class daftar_user extends Activity {
EditText ktp,nama,ttl,alamat,agama,pekerjaan,username,password;
RadioGroup jk;
TextView status;
Button simpan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_user);
ktp=(EditText)findViewById(R.id.txtnomorktp);
nama=(EditText)findViewById(R.id.txtnama);
ttl=(EditText)findViewById(R.id.txtttl);
jk=(RadioGroup) findViewById(R.id.jekel);
alamat=(EditText)findViewById(R.id.txtalamat);
agama=(EditText)findViewById(R.id.txtagama);
pekerjaan=(EditText)findViewById(R.id.txtpekerjaan);
username=(EditText)findViewById(R.id.txtusername);
password=(EditText)findViewById(R.id.txtpassword);
simpan=(Button)findViewById(R.id.btnsimpan);
// keluar=(Button)findViewById(R.id.btnexit);
status=(TextView)findViewById(R.id.txtstatus);
simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//atur variabel utk menampung pilihan jenis kelamin
String type=null;
switch (jk.getCheckedRadioButtonId()) {
case R.id.pria:
type="Pria";
break;
case R.id.perempuan:
type="Perempuan";
break;
}
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("nomor_ktp", ktp.getText().toString()));
postParameters.add(new BasicNameValuePair("nama", nama.getText().toString()));
postParameters.add(new BasicNameValuePair("ttl",ttl.getText().toString()));
postParameters.add(new BasicNameValuePair("alamat", alamat.getText().toString()));
postParameters.add(new BasicNameValuePair("jekel", type));
postParameters.add(new BasicNameValuePair("agama", agama.getText().toString()));
postParameters.add(new BasicNameValuePair("pekerjaan", pekerjaan.getText().toString()));
postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/akta/daftar_user.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) {
username.setText(e.toString());
}
}
});
}
public void keluar (View theButton)
{
Intent a = new Intent (this,login.class);
startActivity(a);
}
}
setelah membuat class daftar_user, sekarang class login.java :
package com.wilis;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class login extends Activity {
/** Called when the activity is first created. */
EditText username,password;
TextView status;
Button login,daftar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username=(EditText) findViewById(R.id.txtusername);
password=(EditText) findViewById(R.id.txtpassword);
status=(TextView) findViewById (R.id.txtstatus);
login=(Button) findViewById (R.id.btnlogin);
daftar=(Button) findViewById (R.id.btndaftar);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/akta/check.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
status.setText(res);
if (res.equals("1"))
{
status.setText("Correct Username or Password");
berhasil(v);
}
else {
status.setText("Sorry!! Wrong Username or Password Entered");
}
}
catch (Exception e) {
status.setText(e.toString());
}
}
});
// penutup buka dari public void onCreate
}
public void daftar (View theButton)
{
Intent d = new Intent (this,daftar_user.class);
startActivity(d);
}
// apabila user berhasil login.
public void berhasil (View theButton)
{
Intent s = new Intent (this, menulayanan.class);
startActivity(s);
}
}
Berikut ini menulayanan :
package com.wilis;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class menulayanan extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] menulayanan = new String[] { "Petunjuk Pendaftaran", "Syarat-syarat pendaftaran", "Form Pendaftaran Kelahiran", "Call Informasi","Exit"};
//Menset nilai array ke dalam list adapater sehingga data pada array akan dimunculkan dalam list
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,menulayanan));
}
@Override
/**method ini akan mengoveride method onListItemClick yang ada pada class List Activity
* method ini akan dipanggil apabilai ada salah satu item dari list menu yang dipilih
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
// Menangkap nilai text yang dklik
Object o = this.getListAdapter().getItem(position);
String pilihan = o.toString();
// Menampilkan hasil pilihan menu dalam bentuk Toast
tampilkanPilihan(pilihan);
}
/**
* Tampilkan Activity sesuai dengan menu yang dipilih
*
*/
protected void tampilkanPilihan(String pilihan) {
try {
//Intent digunakan untuk sebagai pengenal suatu activity
Intent i = null;
if (pilihan.equals("Petunjuk Pendaftaran")) {
i = new Intent(this, login.class);
} else if (pilihan.equals("Syarat-syarat pendaftaran")) {
i = new Intent(this, login.class);
} else if (pilihan.equals("Form Pendaftaran Kelahiran")) {
i = new Intent(this, daftar_akta.class);
} else if (pilihan.equals("Call Informasi")) {
i = new Intent(this, login.class);
} else if (pilihan.equals("Exit")) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure want to Exit?")
.setCancelable(false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent exit = new Intent(
Intent.ACTION_MAIN);
exit.addCategory(Intent.CATEGORY_HOME);
exit
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(exit);
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
}).show();
} else {
Toast.makeText(this,"Anda Memilih: " + pilihan + " , Actionnya belum dibuat", Toast.LENGTH_LONG).show();
}
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
setelah kita membuat class sekarang kita akan membuat Layout. Berikut ini kita akan membuat main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
kemudian kita akan membuat daftar_akta.xml :
sekarang kita akan membuat daftar_user.xml :
layout login.xml :
setelah kita membuat class dan layaoutnya sekarang kita akan menjalankan projectnya. klik kanan pada project lalu Run As -> Android Application.
hasil running :
Sebelum login, terutama mendaftar kemudian kita bisa login kedalam.
Selesai !!!
0 komentar:
Posting Komentar