Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.99 KB | None | 0 0
  1. MainActivity
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.graphics.Typeface;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import com.android.volley.AuthFailureError;
  14. import com.android.volley.Request;
  15. import com.android.volley.Response;
  16. import com.android.volley.VolleyError;
  17. import com.android.volley.toolbox.StringRequest;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import java.util.HashMap;
  24. import java.util.Map;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27. Button login_button;
  28. EditText Username,Password;
  29. String username,password;
  30. String login_url = "http://IP_ADDRS/VolleyAndroid/login.php";
  31. AlertDialog.Builder builder;
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_main);
  37.  
  38. builder = new AlertDialog.Builder(MainActivity.this);
  39. login_button = (Button)findViewById(R.id.login_btn);
  40. Username = (EditText)findViewById(R.id.emailln);
  41. Password = (EditText)findViewById(R.id.passln);
  42. login_button.setOnClickListener(new View.OnClickListener(){
  43.  
  44. @Override
  45. public void onClick(View v){
  46. username = Username.getText().toString();
  47. password = Password.getText().toString();
  48.  
  49. if(username.equals("") || password.equals("")){
  50. builder.setTitle("Something Went wrong");
  51. displayAlert("Enter a valid username and password");
  52. }
  53. else{
  54. StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
  55. new Response.Listener<String>(){
  56. @Override
  57. public void onResponse(String response){
  58.  
  59. try {
  60. JSONArray jsonArray = new JSONArray(response);
  61. JSONObject jsonObject = jsonArray.getJSONObject(0);
  62. String code = jsonObject.getString("code");
  63. if(code.equals("login_failed")){
  64. builder.setTitle("Login Error");
  65. displayAlert(jsonObject.getString("message"));
  66. }
  67. else{
  68. Intent intent = new Intent (MainActivity.this,LoginSuccess.class);
  69. Bundle bundle = new Bundle();
  70. bundle.putString("name",jsonObject.getString("name"));
  71. bundle.putString("email",jsonObject.getString("email"));
  72. intent.putExtras(bundle);
  73. startActivity(intent);
  74.  
  75. }
  76.  
  77. } catch (JSONException e) {
  78. e.printStackTrace();
  79. }
  80.  
  81. }
  82. }, new Response.ErrorListener(){
  83. @Override
  84. public void onErrorResponse(VolleyError error){
  85.  
  86. }
  87.  
  88. })
  89. {
  90. @Override
  91. protected Map<String, String> getParams() throws AuthFailureError {
  92. Map<String,String> params = new HashMap<String, String>();
  93. params.put("user_name",username);
  94. params.put("password",password);
  95. return params;
  96. }
  97. };
  98. MySingleton.getInstance(MainActivity.this).addToRequest(stringRequest);
  99. }
  100. }
  101. });
  102.  
  103. }
  104.  
  105. public void displayAlert(String message){
  106. builder.setMessage(message);
  107. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  108. @Override
  109. public void onClick(DialogInterface dialog, int which) {
  110. Username.setText("");
  111. Password.setText("");
  112. }
  113. });
  114.  
  115. AlertDialog alertDialog = builder.create();
  116. alertDialog.show();
  117. }
  118.  
  119. public void register(View v){
  120. Intent intent = new Intent(getApplicationContext(),Register.class);
  121. startActivity(intent);
  122. }
  123. }
  124.  
  125. Register
  126. import android.content.DialogInterface;
  127. import android.support.v7.app.AlertDialog;
  128. import android.support.v7.app.AppCompatActivity;
  129. import android.os.Bundle;
  130. import android.view.View;
  131. import android.widget.Button;
  132. import android.widget.EditText;
  133.  
  134. import com.android.volley.AuthFailureError;
  135. import com.android.volley.Request;
  136. import com.android.volley.Response;
  137. import com.android.volley.VolleyError;
  138. import com.android.volley.toolbox.StringRequest;
  139.  
  140. import org.json.JSONArray;
  141. import org.json.JSONException;
  142. import org.json.JSONObject;
  143.  
  144. import java.util.HashMap;
  145. import java.util.Map;
  146.  
  147. public class Register extends AppCompatActivity {
  148. Button reg_bn;
  149. EditText Name,Phone,Email,Password,ConPassword;
  150. String name,phone,email,password,conpass;
  151. AlertDialog.Builder builder;
  152.  
  153. String reg_url = "http://IP_ADDRS/VolleyAndroid/register.php";
  154.  
  155.  
  156. @Override
  157. protected void onCreate(Bundle savedInstanceState) {
  158. super.onCreate(savedInstanceState);
  159. setContentView(R.layout.activity_register);
  160. reg_bn = (Button)findViewById(R.id.registerbtn);
  161. Name = (EditText)findViewById(R.id.nameedit);
  162. Phone = (EditText)findViewById(R.id.phoneedit);
  163. Email = (EditText)findViewById(R.id.emailedit);
  164. Password = (EditText)findViewById(R.id.passedit);
  165. ConPassword = (EditText)findViewById(R.id.cpassedit);
  166. builder = new AlertDialog.Builder(Register.this);
  167. reg_bn.setOnClickListener(new View.OnClickListener(){
  168.  
  169. public void onClick(View v)
  170. {
  171. name = Name.getText().toString();
  172. phone = Phone.getText().toString();
  173. email = Email.getText().toString();
  174. password = Password.getText().toString();
  175. conpass = ConPassword.getText().toString();
  176.  
  177. if(name.equals("") || phone.equals("") || email.equals("") || password.equals("") || conpass.equals("")){
  178. builder.setTitle("Something went Wrng!!");
  179. builder.setMessage("Please enter all the feilds");
  180. displayAlert("input_err");
  181. }
  182.  
  183. else{
  184. if(!(password.equals(conpass))){
  185. builder.setTitle("Something went wrong!!");
  186. builder.setMessage("Password incorrect!");
  187. displayAlert("input_err");
  188. }
  189. else{
  190. StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url,
  191. new Response.Listener<String>() {
  192. @Override
  193. public void onResponse(String response) {
  194. try {
  195. JSONArray jsonArray = new JSONArray(response);
  196. JSONObject jsonObject = jsonArray.getJSONObject(0);
  197. String code = jsonObject.getString("code");
  198. String message = jsonObject.getString("message");
  199. builder.setTitle("Response from server");
  200. builder.setMessage(message);
  201. displayAlert(code);
  202. } catch (JSONException e) {
  203. e.printStackTrace();
  204. }
  205.  
  206. }
  207. }, new Response.ErrorListener(){
  208. public void onErrorResponse(VolleyError error){
  209.  
  210. }
  211. }){
  212. @Override
  213. protected Map<String, String> getParams() throws AuthFailureError{
  214. Map<String, String> params =new HashMap<String, String>();
  215. params.put("name",name);
  216. params.put("phone",phone);
  217. params.put("email",email);
  218. params.put("pass",password);
  219.  
  220.  
  221. return params;
  222. }
  223. };
  224.  
  225. MySingleton.getInstance(Register.this).addToRequest(stringRequest);
  226. }
  227. }
  228. }
  229. });
  230. }
  231.  
  232. public void displayAlert(final String code){
  233. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  234. @Override
  235. public void onClick(DialogInterface dialog, int which) {
  236. if(code.equals("input_error"))
  237. {
  238. Password.setText("");
  239. ConPassword.setText("");
  240. }
  241. else if(code.equals("reg_success"))
  242. {
  243. finish();
  244. }
  245. else if(code.equals("reg_failed")){
  246. Name.setText("");
  247. Phone.setText("");
  248. Email.setText("");
  249. Password.setText("");
  250. ConPassword.setText("");
  251. }
  252. }
  253. });
  254. AlertDialog alertDialog = builder.create();
  255. alertDialog.show();
  256. }
  257. }
  258.  
  259. MySingleton
  260. import android.content.Context;
  261.  
  262. import com.android.volley.Request;
  263. import com.android.volley.RequestQueue;
  264. import com.android.volley.toolbox.Volley;
  265.  
  266. /**
  267. * Created by sshank on 5/1/17.
  268. */
  269.  
  270. public class MySingleton {
  271. private static MySingleton mInstance;
  272. private RequestQueue requestQueue;
  273. private static Context mctx;
  274. private MySingleton(Context context){
  275. mctx = context;
  276. requestQueue = getRequestQueue();
  277. }
  278.  
  279. private RequestQueue getRequestQueue() {
  280. if(requestQueue == null)
  281. {
  282. requestQueue = Volley.newRequestQueue(mctx.getApplicationContext());
  283.  
  284. }
  285. return requestQueue;
  286. }
  287.  
  288. public static synchronized MySingleton getInstance(Context context)
  289. {
  290. if(mInstance == null)
  291. {
  292. mInstance = new MySingleton(context);
  293. }
  294. return mInstance;
  295. }
  296.  
  297. public <T>void addToRequest(Request<T> request)
  298. {
  299. requestQueue.add(request);
  300. }
  301. }
  302.  
  303. My layout files
  304. activity_main
  305. <?xml version="1.0" encoding="utf-8"?>
  306. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  307. xmlns:tools="http://schemas.android.com/tools"
  308. android:id="@+id/activity_main"
  309. android:layout_width="match_parent"
  310. android:layout_height="match_parent"
  311. android:paddingBottom="@dimen/activity_vertical_margin"
  312. android:paddingLeft="@dimen/activity_horizontal_margin"
  313. android:paddingRight="@dimen/activity_horizontal_margin"
  314. android:paddingTop="@dimen/activity_vertical_margin"
  315. tools:context="com.company.sshank.volleyimplementation.MainActivity">
  316.  
  317. <TextView
  318. android:id="@+id/title"
  319. android:layout_width="match_parent"
  320. android:layout_height="wrap_content"
  321. android:text="Easy Travel Connect"
  322. android:textAlignment="center"
  323. android:textSize="25dp"
  324. android:layout_marginTop="35dp"
  325. />
  326. <RelativeLayout
  327. android:layout_height="wrap_content"
  328. android:layout_width="match_parent"
  329. android:id="@+id/block1"
  330. android:layout_below="@+id/title"
  331. android:layout_marginTop="50dp">
  332. <TextView
  333. android:id="@+id/emailtxt"
  334. android:layout_width="match_parent"
  335. android:layout_height="wrap_content"
  336. android:text="Email"></TextView>
  337.  
  338. <EditText
  339. android:id="@+id/emailln"
  340. android:layout_height="wrap_content"
  341. android:layout_width="match_parent"
  342. android:layout_below="@+id/emailtxt"></EditText>
  343.  
  344. <TextView
  345. android:id="@+id/passtxt"
  346. android:layout_width="match_parent"
  347. android:layout_height="wrap_content"
  348. android:text="Password"
  349. android:layout_below="@+id/emailln"></TextView>
  350.  
  351. <EditText
  352. android:id="@+id/passln"
  353. android:layout_height="wrap_content"
  354. android:layout_width="match_parent"
  355. android:layout_below="@+id/passtxt"></EditText>
  356.  
  357. <Button
  358. android:id="@+id/login_btn"
  359. android:layout_width="match_parent"
  360. android:layout_height="wrap_content"
  361. android:layout_below="@+id/passln"
  362. android:layout_marginTop="25dp"
  363. android:text="Sign In"></Button>
  364. </RelativeLayout>
  365.  
  366. <RelativeLayout
  367. android:layout_height="wrap_content"
  368. android:layout_width="match_parent"
  369. android:id="@+id/block2"
  370. android:layout_below="@+id/block1">
  371.  
  372. <TextView android:layout_height="wrap_content"
  373. android:layout_width="match_parent"
  374. android:id="@+id/registerlink"
  375. android:text="Not a user? Connect now!"
  376. android:onClick="register"
  377. android:gravity="center"
  378. android:layout_marginTop="10dp"/>
  379.  
  380. <TextView android:layout_height="wrap_content"
  381. android:layout_width="match_parent"
  382. android:id="@+id/terms"
  383. android:layout_below="@+id/registerlink"
  384. android:text="I Agree to the terms and conditions"
  385. android:layout_marginTop="120dp"
  386. android:textStyle="bold"
  387. android:gravity="center" />
  388. </RelativeLayout>
  389. </RelativeLayout>
  390.  
  391. activity_register
  392. <?xml version="1.0" encoding="utf-8"?>
  393. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  394. xmlns:tools="http://schemas.android.com/tools"
  395. android:id="@+id/activity_register"
  396. android:layout_width="match_parent"
  397. android:layout_height="match_parent"
  398. android:paddingBottom="@dimen/activity_vertical_margin"
  399. android:paddingLeft="@dimen/activity_horizontal_margin"
  400. android:paddingRight="@dimen/activity_horizontal_margin"
  401. android:paddingTop="@dimen/activity_vertical_margin"
  402. tools:context="com.company.sshank.volleyimplementation.Register">
  403.  
  404.  
  405. <TextView
  406. android:id="@+id/title"
  407. android:layout_width="match_parent"
  408. android:layout_height="wrap_content"
  409. android:text="Easy Travel Connect"
  410. android:textAlignment="center"
  411. android:textSize="25dp"
  412. android:layout_marginTop="25dp"
  413. />
  414. <RelativeLayout
  415. android:layout_height="wrap_content"
  416. android:layout_width="match_parent"
  417. android:id="@+id/block1"
  418. android:layout_below="@+id/title"
  419. android:layout_marginTop="30dp">
  420.  
  421. <TextView
  422. android:id="@+id/nametxt"
  423. android:layout_width="match_parent"
  424. android:layout_height="wrap_content"
  425. android:text="Name"></TextView>
  426.  
  427. <EditText
  428. android:id="@+id/nameedit"
  429. android:layout_height="wrap_content"
  430. android:layout_width="match_parent"
  431. android:layout_below="@+id/nametxt"></EditText>
  432.  
  433. <TextView
  434. android:id="@+id/phonetxt"
  435. android:layout_width="match_parent"
  436. android:layout_height="wrap_content"
  437. android:text="Phone"
  438. android:layout_below="@+id/nameedit"></TextView>
  439.  
  440. <EditText
  441. android:id="@+id/phoneedit"
  442. android:layout_height="wrap_content"
  443. android:layout_width="match_parent"
  444. android:layout_below="@+id/phonetxt"></EditText>
  445.  
  446. <TextView
  447. android:id="@+id/emailtxt"
  448. android:layout_width="match_parent"
  449. android:layout_height="wrap_content"
  450. android:text="Email"
  451. android:layout_below="@+id/phoneedit"></TextView>
  452.  
  453. <EditText
  454. android:id="@+id/emailedit"
  455. android:layout_height="wrap_content"
  456. android:layout_width="match_parent"
  457. android:layout_below="@+id/emailtxt"></EditText>
  458.  
  459. <TextView
  460. android:id="@+id/passtxt"
  461. android:layout_width="match_parent"
  462. android:layout_height="wrap_content"
  463. android:text="Password"
  464. android:layout_below="@+id/emailedit"></TextView>
  465.  
  466. <EditText
  467. android:id="@+id/passedit"
  468. android:layout_height="wrap_content"
  469. android:layout_width="match_parent"
  470. android:layout_below="@+id/passtxt"></EditText>
  471.  
  472. <TextView
  473. android:id="@+id/cpasstxt"
  474. android:layout_width="match_parent"
  475. android:layout_height="wrap_content"
  476. android:text="Confirm Password"
  477. android:layout_below="@+id/passedit"></TextView>
  478.  
  479. <EditText
  480. android:id="@+id/cpassedit"
  481. android:layout_height="wrap_content"
  482. android:layout_width="match_parent"
  483. android:layout_below="@+id/cpasstxt"></EditText>
  484.  
  485. <Button
  486. android:id="@+id/registerbtn"
  487. android:layout_width="match_parent"
  488. android:layout_height="wrap_content"
  489. android:layout_below="@+id/cpassedit"
  490. android:layout_marginTop="15dp"
  491. android:text="Register"></Button>
  492. </RelativeLayout>
  493.  
  494. <RelativeLayout
  495. android:layout_height="wrap_content"
  496. android:layout_width="match_parent"
  497. android:id="@+id/block2"
  498. android:layout_below="@+id/block1">
  499.  
  500. <TextView android:layout_height="wrap_content"
  501. android:layout_width="match_parent"
  502. android:id="@+id/terms"
  503. android:layout_below="@+id/registerlink"
  504. android:text="I Agree to the terms and conditions"
  505. android:layout_marginTop="10dp"
  506. android:textStyle="bold"
  507. android:gravity="center" />
  508. </RelativeLayout>
  509.  
  510.  
  511. </RelativeLayout>
  512.  
  513. Php files.. here im retrieving and inserting data
  514. login
  515. <?php
  516. $host = "localhost";
  517. $user = "username";
  518. $pass = "";
  519. $db = "db_name";
  520. $conn = mysqli_connect($host, $user, $pass, $db);
  521.  
  522.  
  523. $email = $_POST['email'];
  524. $password = $_POST['password'];
  525.  
  526.  
  527. $query = "select * from Login where email like '".$email."'and password like '".$password."';";
  528. $result = mysqli_query($conn, $query);
  529. $response = array();
  530.  
  531. if(mysqli_num_rows($result)>0)
  532. {
  533. $row = mysqli_fetch_row($result);
  534. $name = $row[0];
  535. $phone = $row[1];
  536. $code = "login_success";
  537. array_push($response, array("code"=>$code,"name"=>$name,"phone"=>$phone));
  538. echo json_encode($response);
  539. }else{
  540. $code = "login_failed";
  541. $message = "User not found...Please try again...";
  542. array_push($response, array("code"=>$code,"message"=>$message));
  543. echo json_encode($response);
  544. }
  545.  
  546.  
  547. mysqli_close($conn);
  548. ?>
  549.  
  550. Register
  551. <?php
  552. $host = "localhost";
  553. $user = "username";
  554. $pass = "";
  555. $db = "dbname";
  556.  
  557. $conn = mysqli_connect($host, $user, $pass, $db);
  558.  
  559. $name = $_POST['name'];
  560. $phone = $_POST['phone'];
  561. $email = $_POST['email'];
  562. $password = $_POST['password'];
  563.  
  564.  
  565. $query = "select * from Login where email like '".$email."';";
  566. $result = mysqli_query($conn, $query);
  567. $response = array();
  568.  
  569. if(mysqli_num_rows($result)>0)
  570. {
  571. $code = "reg_failed";
  572. $message = "User exist";
  573. array_push($response, array("code"=>$code,"message"=>$message));
  574. echo json_encode($response);
  575. }else{
  576. $query = "insert into Login (name,phone,email,password)
  577. values ('".$name."', '".$phone."', '".$email."', '".$password."')";
  578. $result = mysqli_query($conn, $query);
  579. $code = "reg_success";
  580. $message = "Thank you for rgistering with us!! Now you can login!";
  581. array_push($response, array("code"=>$code,"message"=>$message));
  582. echo json_encode($response);
  583. }
  584.  
  585. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement