Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.72 KB | None | 0 0
  1. {"USERDETAIL DETAIL":[{"post":{"id":"42","fname":"hanif","lname":"khan","address":"Dhok Saiydan Road, Rawalpindi City, Pakistan","cellno":"1234","phone":"23444","email":"hukhan.surrani@gmail.com","experience":"6 year","category":"Auto","subcategory":"Auto Painting","password":"123","latitude":"33.570858","longitude":"73.048553","dated":"2015-09-11 12:10:23","last_loggedin":"2015-10-02 05:56:08","status":"1","last_profile_updated":"0000-00-00 00:00:00","active_code":"0","profile_image":"1441969837test.png"}}
  2.  
  3. {
  4. "USERDETAIL DETAIL": [
  5. {
  6. "post": {
  7. "id": "42",
  8. "fname": "hanif",
  9. "lname": "khan",
  10. "address": "Dhok Saiydan Road, Rawalpindi City, Pakistan",
  11. "cellno": "1234",
  12. "phone": "23444",
  13. "email": "hukhan.surrani@gmail.com",
  14. "experience": "6 year",
  15. "category": "Auto",
  16. "subcategory": "Auto Painting",
  17. "password": "123",
  18. "latitude": "33.570858",
  19. "longitude": "73.048553",
  20. "dated": "2015-09-11 12:10:23",
  21. "last_loggedin": "2015-10-02 05:56:08",
  22. "status": "1",
  23. "last_profile_updated": "0000-00-00 00:00:00",
  24. "active_code": "0",
  25. "profile_image": "1441969837test.png"
  26. }
  27. }
  28. ]
  29. }
  30.  
  31. import java.util.List;
  32.  
  33. /**
  34. * Created by Angad Tiwari on 10/5/15.
  35. */
  36. public class AppUsers {
  37. /**
  38. * userdetail : [{"post":{"id":"42","fname":"hanif","lname":"khan","address":"Dhok Saiydan Road, Rawalpindi City, Pakistan","cellno":"1234","phone":"23444","email":"hukhan.surrani@gmail.com","experience":"6 year","category":"Auto","subcategory":"Auto Painting","password":"123","latitude":"33.570858","longitude":"73.048553","dated":"2015-09-11 12:10:23","last_loggedin":"2015-10-02 05:56:08","status":"1","last_profile_updated":"0000-00-00 00:00:00","active_code":"0","profile_image":"1441969837test.png"}}]
  39. */
  40.  
  41. private List<UserdetailEntity> userdetail;
  42.  
  43. public void setUserdetail(List<UserdetailEntity> userdetail) {
  44. this.userdetail = userdetail;
  45. }
  46.  
  47. public List<UserdetailEntity> getUserdetail() {
  48. return userdetail;
  49. }
  50.  
  51. public static class UserdetailEntity {
  52. /**
  53. * post : {"id":"42","fname":"hanif","lname":"khan","address":"Dhok Saiydan Road, Rawalpindi City, Pakistan","cellno":"1234","phone":"23444","email":"hukhan.surrani@gmail.com","experience":"6 year","category":"Auto","subcategory":"Auto Painting","password":"123","latitude":"33.570858","longitude":"73.048553","dated":"2015-09-11 12:10:23","last_loggedin":"2015-10-02 05:56:08","status":"1","last_profile_updated":"0000-00-00 00:00:00","active_code":"0","profile_image":"1441969837test.png"}
  54. */
  55.  
  56. private PostEntity post;
  57.  
  58. public void setPost(PostEntity post) {
  59. this.post = post;
  60. }
  61.  
  62. public PostEntity getPost() {
  63. return post;
  64. }
  65.  
  66. public static class PostEntity {
  67. /**
  68. * id : 42
  69. * fname : hanif
  70. * lname : khan
  71. * address : Dhok Saiydan Road, Rawalpindi City, Pakistan
  72. * cellno : 1234
  73. * phone : 23444
  74. * email : hukhan.surrani@gmail.com
  75. * experience : 6 year
  76. * category : Auto
  77. * subcategory : Auto Painting
  78. * password : 123
  79. * latitude : 33.570858
  80. * longitude : 73.048553
  81. * dated : 2015-09-11 12:10:23
  82. * last_loggedin : 2015-10-02 05:56:08
  83. * status : 1
  84. * last_profile_updated : 0000-00-00 00:00:00
  85. * active_code : 0
  86. * profile_image : 1441969837test.png
  87. */
  88.  
  89. private String id;
  90. private String fname;
  91. private String lname;
  92. private String address;
  93. private String cellno;
  94. private String phone;
  95. private String email;
  96. private String experience;
  97. private String category;
  98. private String subcategory;
  99. private String password;
  100. private String latitude;
  101. private String longitude;
  102. private String dated;
  103. private String last_loggedin;
  104. private String status;
  105. private String last_profile_updated;
  106. private String active_code;
  107. private String profile_image;
  108.  
  109. public void setId(String id) {
  110. this.id = id;
  111. }
  112.  
  113. public void setFname(String fname) {
  114. this.fname = fname;
  115. }
  116.  
  117. public void setLname(String lname) {
  118. this.lname = lname;
  119. }
  120.  
  121. public void setAddress(String address) {
  122. this.address = address;
  123. }
  124.  
  125. public void setCellno(String cellno) {
  126. this.cellno = cellno;
  127. }
  128.  
  129. public void setPhone(String phone) {
  130. this.phone = phone;
  131. }
  132.  
  133. public void setEmail(String email) {
  134. this.email = email;
  135. }
  136.  
  137. public void setExperience(String experience) {
  138. this.experience = experience;
  139. }
  140.  
  141. public void setCategory(String category) {
  142. this.category = category;
  143. }
  144.  
  145. public void setSubcategory(String subcategory) {
  146. this.subcategory = subcategory;
  147. }
  148.  
  149. public void setPassword(String password) {
  150. this.password = password;
  151. }
  152.  
  153. public void setLatitude(String latitude) {
  154. this.latitude = latitude;
  155. }
  156.  
  157. public void setLongitude(String longitude) {
  158. this.longitude = longitude;
  159. }
  160.  
  161. public void setDated(String dated) {
  162. this.dated = dated;
  163. }
  164.  
  165. public void setLast_loggedin(String last_loggedin) {
  166. this.last_loggedin = last_loggedin;
  167. }
  168.  
  169. public void setStatus(String status) {
  170. this.status = status;
  171. }
  172.  
  173. public void setLast_profile_updated(String last_profile_updated) {
  174. this.last_profile_updated = last_profile_updated;
  175. }
  176.  
  177. public void setActive_code(String active_code) {
  178. this.active_code = active_code;
  179. }
  180.  
  181. public void setProfile_image(String profile_image) {
  182. this.profile_image = profile_image;
  183. }
  184.  
  185. public String getId() {
  186. return id;
  187. }
  188.  
  189. public String getFname() {
  190. return fname;
  191. }
  192.  
  193. public String getLname() {
  194. return lname;
  195. }
  196.  
  197. public String getAddress() {
  198. return address;
  199. }
  200.  
  201. public String getCellno() {
  202. return cellno;
  203. }
  204.  
  205. public String getPhone() {
  206. return phone;
  207. }
  208.  
  209. public String getEmail() {
  210. return email;
  211. }
  212.  
  213. public String getExperience() {
  214. return experience;
  215. }
  216.  
  217. public String getCategory() {
  218. return category;
  219. }
  220.  
  221. public String getSubcategory() {
  222. return subcategory;
  223. }
  224.  
  225. public String getPassword() {
  226. return password;
  227. }
  228.  
  229. public String getLatitude() {
  230. return latitude;
  231. }
  232.  
  233. public String getLongitude() {
  234. return longitude;
  235. }
  236.  
  237. public String getDated() {
  238. return dated;
  239. }
  240.  
  241. public String getLast_loggedin() {
  242. return last_loggedin;
  243. }
  244.  
  245. public String getStatus() {
  246. return status;
  247. }
  248.  
  249. public String getLast_profile_updated() {
  250. return last_profile_updated;
  251. }
  252.  
  253. public String getActive_code() {
  254. return active_code;
  255. }
  256.  
  257. public String getProfile_image() {
  258. return profile_image;
  259. }
  260. }
  261. }
  262. }
  263.  
  264. Gson gson=new Gson();
  265. AppUsers appUsersObject=gson.fromJson(<your json>.toString(), AppUsers.class);
  266.  
  267. PostEntity postEntity=new PostEntity();
  268. postEntity.setId("newid"); // add all the attributes of postentity
  269. UserdetailEntity userdetail=new UserdetailEntity();
  270. userdetail.setPost(postEntity);
  271. appUsersObject.getUserdetail().add(userdetail);
  272.  
  273. public class LoginActivity extends Activity {
  274. private static final String TAG = RegisterActivity.class.getSimpleName();
  275. private Button btnLogin;
  276. private Button btnLinkToRegister;
  277. private EditText inputEmail;
  278. private EditText inputPassword;
  279. private ProgressDialog pDialog;
  280. private SessionManager session;
  281. private SQLiteHandler db;
  282.  
  283. @Override
  284. public void onCreate(Bundle savedInstanceState) {
  285. super.onCreate(savedInstanceState);
  286. setContentView(R.layout.activity_login);
  287.  
  288. inputEmail = (EditText) findViewById(R.id.email);
  289. inputPassword = (EditText) findViewById(R.id.password);
  290. btnLogin = (Button) findViewById(R.id.btnLogin);
  291. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  292.  
  293. // Progress dialog
  294. pDialog = new ProgressDialog(this);
  295. pDialog.setCancelable(false);
  296.  
  297. // SQLite database handler
  298. db = new SQLiteHandler(getApplicationContext());
  299.  
  300. // Session manager
  301. session = new SessionManager(getApplicationContext());
  302.  
  303. // Check if user is already logged in or not
  304. if (session.isLoggedIn()) {
  305. // User is already logged in. Take him to main activity
  306. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  307. startActivity(intent);
  308. finish();
  309. }
  310.  
  311. // Login button Click Event
  312. btnLogin.setOnClickListener(new View.OnClickListener() {
  313.  
  314. public void onClick(View view) {
  315. String email = inputEmail.getText().toString().trim();
  316. String password = inputPassword.getText().toString().trim();
  317.  
  318. // Check for empty data in the form
  319. if (!email.isEmpty() && !password.isEmpty()) {
  320. // login user
  321. checkLogin(email, password);
  322. } else {
  323. // Prompt user to enter credentials
  324. Toast.makeText(getApplicationContext(),
  325. "Please enter the credentials!", Toast.LENGTH_LONG)
  326. .show();
  327. }
  328. }
  329.  
  330. });
  331.  
  332. // Link to Register Screen
  333. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  334.  
  335. public void onClick(View view) {
  336. Intent i = new Intent(getApplicationContext(),
  337. RegisterActivity.class);
  338. startActivity(i);
  339. finish();
  340. }
  341. });
  342.  
  343. }
  344.  
  345. /**
  346. * function to verify login details in mysql db
  347. * */
  348. private void checkLogin(final String email, final String password) {
  349. // Tag used to cancel the request
  350. String tag_string_req = "req_login";
  351.  
  352. pDialog.setMessage("Logging in ...");
  353. showDialog();
  354.  
  355. StringRequest strReq = new StringRequest(Method.POST,
  356. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  357.  
  358. @Override
  359. public void onResponse(String response) {
  360. Log.d(TAG, "Login Response: " + response.toString());
  361. hideDialog();
  362.  
  363. try {
  364. JSONObject jObj = new JSONObject(response);
  365. boolean error = jObj.getBoolean("error");
  366.  
  367. // Check for error node in json
  368. if (!error) {
  369. // user successfully logged in
  370. // Create login session
  371. session.setLogin(true);
  372.  
  373. // Now store the user in SQLite
  374. String uid = jObj.getString("uid");
  375.  
  376. JSONObject user = jObj.getJSONObject("user");
  377. String name = user.getString("name");
  378. String email = user.getString("email");
  379. String created_at = user
  380. .getString("created_at");
  381.  
  382. // Inserting row in users table
  383. db.addUser(name, email, uid, created_at);
  384.  
  385. // Launch main activity
  386. Intent intent = new Intent(LoginActivity.this,
  387. MainActivity.class);
  388. startActivity(intent);
  389. finish();
  390. } else {
  391. // Error in login. Get the error message
  392. String errorMsg = jObj.getString("error_msg");
  393. Toast.makeText(getApplicationContext(),
  394. errorMsg, Toast.LENGTH_LONG).show();
  395. }
  396. } catch (JSONException e) {
  397. // JSON error
  398. e.printStackTrace();
  399. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  400. }
  401.  
  402. }
  403. }, new Response.ErrorListener() {
  404.  
  405. @Override
  406. public void onErrorResponse(VolleyError error) {
  407. Log.e(TAG, "Login Error: " + error.getMessage());
  408. Toast.makeText(getApplicationContext(),
  409. error.getMessage(), Toast.LENGTH_LONG).show();
  410. hideDialog();
  411. }
  412. }) {
  413.  
  414. @Override
  415. protected Map<String, String> getParams() {
  416. // Posting parameters to login url
  417. Map<String, String> params = new HashMap<String, String>();
  418. params.put("email", email);
  419. params.put("password", password);
  420.  
  421. return params;
  422. }
  423.  
  424. };
  425.  
  426. // Adding request to request queue
  427. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  428. }
  429.  
  430. private void showDialog() {
  431. if (!pDialog.isShowing())
  432. pDialog.show();
  433. }
  434.  
  435. private void hideDialog() {
  436. if (pDialog.isShowing())
  437. pDialog.dismiss();
  438. }
  439. }
  440.  
  441. public class RegisterActivity extends Activity {
  442. private static final String TAG = RegisterActivity.class.getSimpleName();
  443. private Button btnRegister;
  444. private Button btnLinkToLogin;
  445. private EditText inputFullName;
  446. private EditText inputEmail;
  447. private EditText inputPassword;
  448. private ProgressDialog pDialog;
  449. private SessionManager session;
  450. private SQLiteHandler db;
  451.  
  452. @Override
  453. public void onCreate(Bundle savedInstanceState) {
  454. super.onCreate(savedInstanceState);
  455. setContentView(R.layout.activity_register);
  456.  
  457. inputFullName = (EditText) findViewById(R.id.name);
  458. inputEmail = (EditText) findViewById(R.id.email);
  459. inputPassword = (EditText) findViewById(R.id.password);
  460. btnRegister = (Button) findViewById(R.id.btnRegister);
  461. btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
  462.  
  463. // Progress dialog
  464. pDialog = new ProgressDialog(this);
  465. pDialog.setCancelable(false);
  466.  
  467. // Session manager
  468. session = new SessionManager(getApplicationContext());
  469.  
  470. // SQLite database handler
  471. db = new SQLiteHandler(getApplicationContext());
  472.  
  473. // Check if user is already logged in or not
  474. if (session.isLoggedIn()) {
  475. // User is already logged in. Take him to main activity
  476. Intent intent = new Intent(RegisterActivity.this,
  477. MainActivity.class);
  478. startActivity(intent);
  479. finish();
  480. }
  481.  
  482. // Register Button Click event
  483. btnRegister.setOnClickListener(new View.OnClickListener() {
  484. public void onClick(View view) {
  485. String name = inputFullName.getText().toString().trim();
  486. String email = inputEmail.getText().toString().trim();
  487. String password = inputPassword.getText().toString().trim();
  488.  
  489. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  490. registerUser(name, email, password);
  491. } else {
  492. Toast.makeText(getApplicationContext(),
  493. "Please enter your details!", Toast.LENGTH_LONG)
  494. .show();
  495. }
  496. }
  497. });
  498.  
  499. // Link to Login Screen
  500. btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
  501.  
  502. public void onClick(View view) {
  503. Intent i = new Intent(getApplicationContext(),
  504. LoginActivity.class);
  505. startActivity(i);
  506. finish();
  507. }
  508. });
  509.  
  510. }
  511.  
  512. /**
  513. * Function to store user in MySQL database will post params(tag, name,
  514. * email, password) to register url
  515. * */
  516. private void registerUser(final String name, final String email,
  517. final String password) {
  518. // Tag used to cancel the request
  519. String tag_string_req = "req_register";
  520.  
  521. pDialog.setMessage("Registering ...");
  522. showDialog();
  523.  
  524. StringRequest strReq = new StringRequest(Method.POST,
  525. AppConfig.URL_REGISTER, new Response.Listener<String>() {
  526.  
  527. @Override
  528. public void onResponse(String response) {
  529. Log.d(TAG, "Register Response: " + response.toString());
  530. hideDialog();
  531.  
  532. try {
  533. JSONObject jObj = new JSONObject(response);
  534. boolean error = jObj.getBoolean("error");
  535. if (!error) {
  536. // User successfully stored in MySQL
  537. // Now store the user in sqlite
  538. String uid = jObj.getString("uid");
  539.  
  540. JSONObject user = jObj.getJSONObject("user");
  541. String name = user.getString("name");
  542. String email = user.getString("email");
  543. String created_at = user
  544. .getString("created_at");
  545.  
  546. // Inserting row in users table
  547. db.addUser(name, email, uid, created_at);
  548.  
  549. Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
  550.  
  551. // Launch login activity
  552. Intent intent = new Intent(
  553. RegisterActivity.this,
  554. LoginActivity.class);
  555. startActivity(intent);
  556. finish();
  557. } else {
  558.  
  559. // Error occurred in registration. Get the error
  560. // message
  561. String errorMsg = jObj.getString("error_msg");
  562. Toast.makeText(getApplicationContext(),
  563. errorMsg, Toast.LENGTH_LONG).show();
  564. }
  565. } catch (JSONException e) {
  566. e.printStackTrace();
  567. }
  568.  
  569. }
  570. }, new Response.ErrorListener() {
  571.  
  572. @Override
  573. public void onErrorResponse(VolleyError error) {
  574. Log.e(TAG, "Registration Error: " + error.getMessage());
  575. Toast.makeText(getApplicationContext(),
  576. error.getMessage(), Toast.LENGTH_LONG).show();
  577. hideDialog();
  578. }
  579. }) {
  580.  
  581. @Override
  582. protected Map<String, String> getParams() {
  583. // Posting params to register url
  584. Map<String, String> params = new HashMap<String, String>();
  585. params.put("name", name);
  586. params.put("email", email);
  587. params.put("password", password);
  588.  
  589. return params;
  590. }
  591.  
  592. };
  593.  
  594. // Adding request to request queue
  595. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  596. }
  597.  
  598. private void showDialog() {
  599. if (!pDialog.isShowing())
  600. pDialog.show();
  601. }
  602.  
  603. private void hideDialog() {
  604. if (pDialog.isShowing())
  605. pDialog.dismiss();
  606. }
  607. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement