Anda di halaman 1dari 8

Tugas Teori Piranti Bergerak

Nama : Muhammad Nur Taufik H


Kelas : 4SI-5
NRP : 6313034

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:fillViewport="false">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login Pengguna"

android:id="@+id/textView"
android:layout_gravity="center_horizontal" />

<EditText android:id="@+id/tId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username" />

<EditText android:id="@+id/tPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>

<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:onClick="post"
android:focusable="true"
android:text="Login"/>
</LinearLayout>
</ScrollView>

Main Activity.java
package tugas.ervanandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void post (View v) {


EditText username = (EditText) findViewById((R.id.tId));
EditText password = (EditText) findViewById((R.id.tPass));

String id = username.getText().toString();
String pass = password.getText().toString();

new getter(this,
"http://192.168.4.100/TugasErvan/_getLoginUser.php?username="+ id
+"&password="+ pass+"",0);

Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="tugas.ervanandroid.Main2Activity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Berhasil Login"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>

" http://192.168.4.100/TugasErvan/_getLoginUser.php?username="+ id +"&password="+


pass+"",0);

}
Main2Activity
package tugas.ervanandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

Link.Java
package tugas.ervanandroid;

public class link {

public static final String TAG_JSON_ARRAY="result";


public static final String TAG_USER="username";
public static final String TAG_PASS="password";

//JSON URL
//Kalo Ga Connect ke Server
public static final String
URL_GET_ALL="http://192.168.4.100/TugasErvan/_getLoginUser.php";

Getter.java
package tugas.ervanandroid;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class getter {

Context context;
String urlcari;
String username;
String password;
ListView listView;

public getter (Context context, String link_json, Integer type) {


this.context = context;
this.urlcari = link_json;
switch (type) {
case 0:
cariLogin(urlcari);
break;

}
}

private void cariLogin (final String link) {


class GetUser extends AsyncTask<Void, Void, String> {
ProgressDialog loading;

@Override
protected String doInBackground(Void... params) {
parser par = new parser();
String s = par.kirimRequestGet(link);
return s;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(context, "Logging In ...",
"Please Wait ...", false, false);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
showLogin(s);
}

}
GetUser ge = new GetUser();
ge.execute();
}

private void showLogin (String json) {


try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result =
jsonObject.getJSONArray(link.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
String name = c.getString(link.TAG_USER);
if (name == "null") {
Toast.makeText(context, "Username atau Password tidak
ditemukan", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Selamat Datang :"+name,
Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, Main2Activity.class);
context.startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

parser.java

package tugas.ervanandroid;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class parser {

public String kirimRequestGet(String requestURL) {


StringBuilder sb = new StringBuilder();
try {
URL link = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection)
link.openConnection();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream())
);
String s;
while ((s=br.readLine()) !=null) {
sb.append(s + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} return sb.toString();
}
}
_Koneksidb
<?php

define ('HOST','localhost');

define ('USER','root');

define ('PASS','');

define ('DB','db_ervan');

$conn = mysqli_connect(HOST,USER,PASS,DB) or die ("Database gagal terhubung. Alasan : ".


mysqli_errno() );

?>

_getLoginUser
<?php

$usr = $_GET['username'];

$pwd = $_GET['password'];

require_once('_koneksidb.php');

$sql = "SELECT * FROM userpengguna WHERE username = '$usr' and password = '$pwd'";

$r = mysqli_query($conn,$sql);

$result = array();

$row = mysqli_fetch_array($r);

array_push($result,array(

"username" =>$row['username'],

"password" =>$row['password'],

));
echo json_encode(array('result'=>$result));

mysqli_close($conn);

?>

Screenshoot Tampilan Login

Screenshoot Berhasil Login


Screenshoot Gagal Login