strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<resources> <string name="app_name">Database Login</string> <string name="action_settings">Settings</string> <string name="hello_world">DB Login!</string> <string name="txtPesan">Silahkan Login terlebih dahulu</string> <string name="txtUser">Username</string> <string name="txtPass">Password</string> <string name="btnLogin">Login</string> <string name="btnSignup">Signup</string> </resources> |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtUser"></TextView> <EditText android:id="@+id/userInput" android:layout_width="match_parent" android:layout_height="wrap_content"> </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtPass"></TextView> <EditText android:id="@+id/passInput" android:layout_width="match_parent" android:layout_height="wrap_content"> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnLogin"></Button> <EditText android:id="@+id/status" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/txtPesan"></EditText> </LinearLayout> |
ClientToServer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
package com.databaselogin; import java.io.BufferedReader; 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 ClientToServer { public static final int HTTP_TIMEOUT = 30 * 1000; private static HttpClient client; private static HttpClient getHttpClient() { if (client == null){ client = new DefaultHttpClient(); final HttpParams parameterHttp = client.getParams(); HttpConnectionParams.setConnectionTimeout(parameterHttp, HTTP_TIMEOUT); } return client; } public static String eksekusiHttpPost(String url, ArrayList<NameValuePair>postParameter) throws Exception { BufferedReader in = null; try { HttpClient klien = getHttpClient(); HttpPost req = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameter); req.setEntity(formEntity); HttpResponse jawaban = klien.execute(req); in = new BufferedReader(new InputStreamReader(jawaban.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 hasil = sb.toString(); return hasil; } finally { if (in != null) { in.close(); } } } public static String eksekusiHttpGet(String url) throws Exception { BufferedReader in = null; try { HttpClient hc = getHttpClient(); HttpGet req = new HttpGet(); req.setURI(new URI(url)); HttpResponse resp = hc.execute(req); in = new BufferedReader(new InputStreamReader(resp.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 hasil = sb.toString(); return hasil; } finally { if (in != null) { in.close(); } } } } |