Cleint Server 13 Membuat from login terkoneksi dengan database dan servernya
Assalam'alaikum Warohmatulahi Wabarokatu..
kali ini saya akan membagikan lanjutan dari cleint server yang saya buat sebelumnya, kali ini tampilannya agak berbeda dari sebelum-sebelumnya Aplikasi ini saya beri judul "APLIKASI PENCARIAN RUMAH KOST BERBASIS ANDROID"
Ok langsung saja !!
1. membuat login
Coding Xmlnya :
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widg37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/log"
android:orientation="vertical" >
<TableLayout
android:id="@+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</TableRow>
<TableRow
android:id="@+id/widget46"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/w7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="@+id/widget65"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</TableRow>
</TableLayout>
<TextView
android:id="@+id/txtstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/widget55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="17dp"
android:layout_y="315dp"
android:text="Password :"
android:textColor="#FF000000" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="96dp"
android:layout_y="301dp"
android:ems="10"
android:password="True"
android:textSize="18sp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtusername"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="93dp"
android:layout_y="244dp"
android:ems="10"
android:textSize="18sp" />
<TextView
android:id="@+id/widget47"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="16dp"
android:layout_y="258dp"
android:text="Username :"
android:textColor="#FF000000" />
<Button
android:id="@+id/btndaftar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btndaftar"
android:layout_x="198dp"
android:layout_y="390dp"
android:onClick="daftar"
android:text="" />
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btnlogin"
android:layout_x="68dp"
android:layout_y="390dp"
android:onClick="login"
android:text="" />
</AbsoluteLayout>
Coding Javanya :
package com.example.kost_app;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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://192.168.100.163/kost/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,Pesan.class);
startActivity(s);
}
}
2. Daftar Usernya
Coding Xmlnya :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget124"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/widget124"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/widget125"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="@+id/widget126"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget127"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Nomor KTP" />
<EditText
android:id="@+id/txtnomorktp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget129"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget130"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Nama" />
<EditText
android:id="@+id/txtnama"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget132"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget133"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Tempat Lahir" />
<EditText
android:id="@+id/txtttl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget135"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget136"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Alamat" />
<EditText
android:id="@+id/txtalamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget138"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget139"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Jenis Kelamin " />
<RadioGroup
android:id="@+id/jekel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/pria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Pria" />
<RadioButton
android:id="@+id/perempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Perempuan" />
</RadioGroup>
</TableRow>
<TableRow
android:id="@+id/widget143"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget144"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Agama" />
<EditText
android:id="@+id/txtagama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget146"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget147"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Pekerjaan" />
<EditText
android:id="@+id/txtpekerjaan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget149"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget150"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Username" />
<EditText
android:id="@+id/txtusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget152"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget153"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="Password" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:password="True"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/widget125"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="@+id/widget155"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnsimpan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIMPAN" />
<Button
android:id="@+id/btnexit"
android:onClick="keluar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E X I T" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/txtstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
Coding Javanya :
package com.example.kost_app;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") 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. */
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_user);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
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://192.168.100.163/kost/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);
}
}
Sekian yang dapat saya bagikan lain kali akan saya lanjutkan lagi dengan transaksinya.
Komentar
Posting Komentar