Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {"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"}}
- {
- "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"
- }
- }
- ]
- }
- import java.util.List;
- /**
- * Created by Angad Tiwari on 10/5/15.
- */
- public class AppUsers {
- /**
- * 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"}}]
- */
- private List<UserdetailEntity> userdetail;
- public void setUserdetail(List<UserdetailEntity> userdetail) {
- this.userdetail = userdetail;
- }
- public List<UserdetailEntity> getUserdetail() {
- return userdetail;
- }
- public static class UserdetailEntity {
- /**
- * 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"}
- */
- private PostEntity post;
- public void setPost(PostEntity post) {
- this.post = post;
- }
- public PostEntity getPost() {
- return post;
- }
- public static class PostEntity {
- /**
- * 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
- */
- private String id;
- private String fname;
- private String lname;
- private String address;
- private String cellno;
- private String phone;
- private String email;
- private String experience;
- private String category;
- private String subcategory;
- private String password;
- private String latitude;
- private String longitude;
- private String dated;
- private String last_loggedin;
- private String status;
- private String last_profile_updated;
- private String active_code;
- private String profile_image;
- public void setId(String id) {
- this.id = id;
- }
- public void setFname(String fname) {
- this.fname = fname;
- }
- public void setLname(String lname) {
- this.lname = lname;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public void setCellno(String cellno) {
- this.cellno = cellno;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public void setExperience(String experience) {
- this.experience = experience;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- public void setSubcategory(String subcategory) {
- this.subcategory = subcategory;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setLatitude(String latitude) {
- this.latitude = latitude;
- }
- public void setLongitude(String longitude) {
- this.longitude = longitude;
- }
- public void setDated(String dated) {
- this.dated = dated;
- }
- public void setLast_loggedin(String last_loggedin) {
- this.last_loggedin = last_loggedin;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public void setLast_profile_updated(String last_profile_updated) {
- this.last_profile_updated = last_profile_updated;
- }
- public void setActive_code(String active_code) {
- this.active_code = active_code;
- }
- public void setProfile_image(String profile_image) {
- this.profile_image = profile_image;
- }
- public String getId() {
- return id;
- }
- public String getFname() {
- return fname;
- }
- public String getLname() {
- return lname;
- }
- public String getAddress() {
- return address;
- }
- public String getCellno() {
- return cellno;
- }
- public String getPhone() {
- return phone;
- }
- public String getEmail() {
- return email;
- }
- public String getExperience() {
- return experience;
- }
- public String getCategory() {
- return category;
- }
- public String getSubcategory() {
- return subcategory;
- }
- public String getPassword() {
- return password;
- }
- public String getLatitude() {
- return latitude;
- }
- public String getLongitude() {
- return longitude;
- }
- public String getDated() {
- return dated;
- }
- public String getLast_loggedin() {
- return last_loggedin;
- }
- public String getStatus() {
- return status;
- }
- public String getLast_profile_updated() {
- return last_profile_updated;
- }
- public String getActive_code() {
- return active_code;
- }
- public String getProfile_image() {
- return profile_image;
- }
- }
- }
- }
- Gson gson=new Gson();
- AppUsers appUsersObject=gson.fromJson(<your json>.toString(), AppUsers.class);
- PostEntity postEntity=new PostEntity();
- postEntity.setId("newid"); // add all the attributes of postentity
- UserdetailEntity userdetail=new UserdetailEntity();
- userdetail.setPost(postEntity);
- appUsersObject.getUserdetail().add(userdetail);
- public class LoginActivity extends Activity {
- private static final String TAG = RegisterActivity.class.getSimpleName();
- private Button btnLogin;
- private Button btnLinkToRegister;
- private EditText inputEmail;
- private EditText inputPassword;
- private ProgressDialog pDialog;
- private SessionManager session;
- private SQLiteHandler db;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- inputEmail = (EditText) findViewById(R.id.email);
- inputPassword = (EditText) findViewById(R.id.password);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
- // Progress dialog
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- // SQLite database handler
- db = new SQLiteHandler(getApplicationContext());
- // Session manager
- session = new SessionManager(getApplicationContext());
- // Check if user is already logged in or not
- if (session.isLoggedIn()) {
- // User is already logged in. Take him to main activity
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- // Login button Click Event
- btnLogin.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- String email = inputEmail.getText().toString().trim();
- String password = inputPassword.getText().toString().trim();
- // Check for empty data in the form
- if (!email.isEmpty() && !password.isEmpty()) {
- // login user
- checkLogin(email, password);
- } else {
- // Prompt user to enter credentials
- Toast.makeText(getApplicationContext(),
- "Please enter the credentials!", Toast.LENGTH_LONG)
- .show();
- }
- }
- });
- // Link to Register Screen
- btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(),
- RegisterActivity.class);
- startActivity(i);
- finish();
- }
- });
- }
- /**
- * function to verify login details in mysql db
- * */
- private void checkLogin(final String email, final String password) {
- // Tag used to cancel the request
- String tag_string_req = "req_login";
- pDialog.setMessage("Logging in ...");
- showDialog();
- StringRequest strReq = new StringRequest(Method.POST,
- AppConfig.URL_LOGIN, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Login Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- // Check for error node in json
- if (!error) {
- // user successfully logged in
- // Create login session
- session.setLogin(true);
- // Now store the user in SQLite
- String uid = jObj.getString("uid");
- JSONObject user = jObj.getJSONObject("user");
- String name = user.getString("name");
- String email = user.getString("email");
- String created_at = user
- .getString("created_at");
- // Inserting row in users table
- db.addUser(name, email, uid, created_at);
- // Launch main activity
- Intent intent = new Intent(LoginActivity.this,
- MainActivity.class);
- startActivity(intent);
- finish();
- } else {
- // Error in login. Get the error message
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- // JSON error
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting parameters to login url
- Map<String, String> params = new HashMap<String, String>();
- params.put("email", email);
- params.put("password", password);
- return params;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- }
- public class RegisterActivity extends Activity {
- private static final String TAG = RegisterActivity.class.getSimpleName();
- private Button btnRegister;
- private Button btnLinkToLogin;
- private EditText inputFullName;
- private EditText inputEmail;
- private EditText inputPassword;
- private ProgressDialog pDialog;
- private SessionManager session;
- private SQLiteHandler db;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- inputFullName = (EditText) findViewById(R.id.name);
- inputEmail = (EditText) findViewById(R.id.email);
- inputPassword = (EditText) findViewById(R.id.password);
- btnRegister = (Button) findViewById(R.id.btnRegister);
- btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
- // Progress dialog
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- // Session manager
- session = new SessionManager(getApplicationContext());
- // SQLite database handler
- db = new SQLiteHandler(getApplicationContext());
- // Check if user is already logged in or not
- if (session.isLoggedIn()) {
- // User is already logged in. Take him to main activity
- Intent intent = new Intent(RegisterActivity.this,
- MainActivity.class);
- startActivity(intent);
- finish();
- }
- // Register Button Click event
- btnRegister.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- String name = inputFullName.getText().toString().trim();
- String email = inputEmail.getText().toString().trim();
- String password = inputPassword.getText().toString().trim();
- if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
- registerUser(name, email, password);
- } else {
- Toast.makeText(getApplicationContext(),
- "Please enter your details!", Toast.LENGTH_LONG)
- .show();
- }
- }
- });
- // Link to Login Screen
- btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(),
- LoginActivity.class);
- startActivity(i);
- finish();
- }
- });
- }
- /**
- * Function to store user in MySQL database will post params(tag, name,
- * email, password) to register url
- * */
- private void registerUser(final String name, final String email,
- final String password) {
- // Tag used to cancel the request
- String tag_string_req = "req_register";
- pDialog.setMessage("Registering ...");
- showDialog();
- StringRequest strReq = new StringRequest(Method.POST,
- AppConfig.URL_REGISTER, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Register Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- if (!error) {
- // User successfully stored in MySQL
- // Now store the user in sqlite
- String uid = jObj.getString("uid");
- JSONObject user = jObj.getJSONObject("user");
- String name = user.getString("name");
- String email = user.getString("email");
- String created_at = user
- .getString("created_at");
- // Inserting row in users table
- db.addUser(name, email, uid, created_at);
- Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
- // Launch login activity
- Intent intent = new Intent(
- RegisterActivity.this,
- LoginActivity.class);
- startActivity(intent);
- finish();
- } else {
- // Error occurred in registration. Get the error
- // message
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Registration Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting params to register url
- Map<String, String> params = new HashMap<String, String>();
- params.put("name", name);
- params.put("email", email);
- params.put("password", password);
- return params;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement