Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/images"
  7. android:orientation="vertical"
  8. tools:context="com.altavista.dc.serviceprovider.MainActivity"
  9. android:weightSum="1"
  10. >
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:text="Username"
  14. android:textSize="18dp"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:layout_marginTop="125dp" />
  18. <EditText
  19. android:id="@+id/etUsername"
  20. android:layout_width="match_parent"
  21. android:layout_marginLeft="10dp"
  22. android:layout_marginRight="10dp"
  23. android:layout_height="wrap_content" />
  24.  
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:text="Password"
  28. android:textSize="18dp"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"/>
  31. <EditText
  32. android:id="@+id/etPassword"
  33. android:layout_width="match_parent"
  34. android:layout_marginLeft="10dp"
  35. android:layout_marginRight="10dp"
  36. android:inputType="textPassword"
  37. android:layout_height="wrap_content" />
  38.  
  39. <Button
  40. android:id="@+id/bLogin"
  41. android:layout_width="match_parent"
  42. android:text="Login"
  43. android:textSize="18dp"
  44. android:layout_height="wrap_content"
  45. android:gravity="center"
  46. android:layout_marginLeft="50dp"
  47. android:layout_marginRight="50dp"
  48. android:layout_marginBottom="10dp"/>
  49. <LinearLayout
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:orientation="horizontal">
  53. <TextView
  54. android:id="@+id/tvForgotlink"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:layout_gravity="center_horizontal"
  58. android:text="Forgot password?"
  59. android:textStyle="bold"
  60. android:layout_marginStart="50dp"
  61. />
  62. <TextView
  63. android:id="@+id/tvRegisterlink"
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:layout_gravity="center_horizontal"
  67. android:text="Register here"
  68. android:textStyle="bold"
  69. android:layout_marginStart="50dp"/>
  70.  
  71. </LinearLayout>
  72.  
  73. import android.content.Intent;
  74. import android.net.Uri;
  75. import android.support.v7.app.AppCompatActivity;
  76. import android.os.Bundle;
  77. import android.view.View;
  78. import android.widget.Button;
  79. import android.widget.EditText;
  80. import android.widget.TextView;
  81. import android.widget.Toast;
  82.  
  83.  
  84. import org.json.JSONArray;
  85. import org.json.JSONException;
  86. import org.json.JSONObject;
  87.  
  88. import java.io.IOException;
  89. import java.util.ArrayList;
  90. import java.util.List;
  91.  
  92. public class MainActivity extends AppCompatActivity implements
  93. View.OnClickListener{
  94.  
  95. Button bLogin;
  96. EditText etUsername,etPassword;
  97. TextView tvRegisterlink,tvForgotlink;
  98.  
  99. @Override
  100. protected void onCreate(Bundle savedInstanceState) {
  101. super.onCreate(savedInstanceState);
  102. setContentView(R.layout.activity_main);
  103.  
  104. etUsername=(EditText)findViewById(R.id.etUsername);
  105. etPassword=(EditText)findViewById(R.id.etPassword);
  106.  
  107. bLogin=(Button)findViewById(R.id.bLogin);
  108.  
  109.  
  110. bLogin.setOnClickListener(this);
  111. tvRegisterlink.setOnClickListener(this);
  112.  
  113. }
  114.  
  115.  
  116. @Override
  117. public void onClick(View v) {
  118.  
  119. String u=etUsername.getText().toString();
  120. String p=etPassword.getText().toString();
  121. String url="http://";
  122.  
  123. JSONObject obj2=new JSONObject();
  124. try {
  125. obj2.put("email",u);
  126. obj2.put("saltedPassword",p);
  127.  
  128. } catch (JSONException e) {
  129. e.printStackTrace();
  130. }
  131.  
  132.  
  133. switch (v.getId()) {
  134.  
  135. case R.id.bLogin:
  136.  
  137.  
  138. try {
  139. JSONObject returnVal=new JSONObject(ConnectionHelper.post(obj2, url));
  140. Boolean val= returnVal.getBoolean("response_bool");
  141.  
  142. if(val){
  143. startActivity(new Intent(this,Home.class));
  144. }else{
  145. Toast.makeText(getApplicationContext(), "Username or password incorrect", Toast.LENGTH_SHORT).show();
  146. }
  147.  
  148. } catch (IOException e) {
  149. e.printStackTrace();
  150. } catch (JSONException e) {
  151. e.printStackTrace();
  152. }
  153.  
  154. break;
  155. case R.id.tvRegisterlink:
  156. startActivity(new Intent(this,Register.class));
  157. break;
  158.  
  159.  
  160. }
  161.  
  162. }
  163.  
  164. package com.altavista.dc.serviceprovider;
  165.  
  166. import android.app.Activity;
  167.  
  168. import org.json.JSONException;
  169. import org.json.JSONObject;
  170.  
  171. import java.io.BufferedReader;
  172. import java.io.DataOutputStream;
  173. import java.io.IOException;
  174. import java.io.InputStreamReader;
  175. import java.net.HttpURLConnection;
  176. import java.net.URL;
  177. import java.net.URLEncoder;
  178.  
  179.  
  180. public class ConnectionHelper {
  181.  
  182. public static String post(JSONObject json, String url) throws IOException, JSONException {
  183.  
  184. URL obj = new URL(url);
  185. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  186.  
  187. //add request header
  188. con.setRequestMethod("POST");
  189. con.setRequestProperty("Content-Type", "application/json");
  190.  
  191.  
  192.  
  193. // Send post request
  194. con.setDoOutput(true);
  195. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  196. wr.writeBytes(json.toString());
  197. wr.flush();
  198. wr.close();
  199.  
  200. if (con.getResponseCode() == 200) {
  201.  
  202. BufferedReader in = new BufferedReader(
  203. new InputStreamReader(con.getInputStream()));
  204. String inputLine;
  205. StringBuffer response = new StringBuffer();
  206.  
  207. while ((inputLine = in.readLine()) != null) {
  208. response.append(inputLine);
  209. }
  210. in.close();
  211. return response.toString();
  212.  
  213. } else {
  214.  
  215. JSONObject jsonObject = new JSONObject();
  216. jsonObject.put("response", con.getResponseCode());
  217. jsonObject.put("message", "error");
  218.  
  219.  
  220. return jsonObject.toString();
  221. }
  222.  
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement