Advertisement
Guest User

Untitled

a guest
Nov 21st, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1.     private void registerUser(final String name, final String email,
  2.                               final String password) {
  3.         // Tag used to cancel the request
  4.         String tag_string_req = "req_register";
  5.  
  6.         pDialog.setMessage("Registering ...");
  7.         showDialog();
  8.  
  9.         StringRequest strReq = new StringRequest(Request.Method.POST,
  10.                 AppConfig.URL_REGISTER, new Response.Listener<String>() {
  11.  
  12.             @Override
  13.             public void onResponse(String response) {
  14.                 Log.d(TAG, "Register Response: " + response.toString());
  15.                 hideDialog();
  16.  
  17.                 try {
  18.                     JSONObject jObj = new JSONObject(response);
  19.                     boolean error = jObj.getBoolean("error");
  20.                     if (!error) {
  21.                         // User successfully stored in MySQL
  22.                         // Now store the user in sqlite
  23.                         String uid = jObj.getString("uid");
  24.  
  25.                         JSONObject user = jObj.getJSONObject("user");
  26.                         String name = user.getString("name");
  27.                         String email = user.getString("email");
  28.                         String created_at = user
  29.                                 .getString("created_at");
  30.  
  31.                         // Inserting row in users table
  32.                         db.addUser(name, email, uid, created_at);
  33.  
  34.                         Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
  35.  
  36.                         // Launch login activity
  37.                         Intent intent = new Intent(
  38.                                 RegisterActivity.this,
  39.                                 LoginActivity.class);
  40.                         startActivity(intent);
  41.                         finish();
  42.                     } else {
  43.  
  44.                         // Error occurred in registration. Get the error
  45.                         // message
  46.                         String errorMsg = jObj.getString("error_msg");
  47.                         Toast.makeText(getApplicationContext(),
  48.                                 errorMsg, Toast.LENGTH_LONG).show();
  49.                     }
  50.                 } catch (JSONException e) {
  51.                     e.printStackTrace();
  52.                 }
  53.  
  54.             }
  55.         }, new Response.ErrorListener() {
  56.  
  57.             @Override
  58.             public void onErrorResponse(VolleyError error) {
  59.                 Log.e(TAG, "Registration Error: " + error.getMessage());
  60.                 Toast.makeText(getApplicationContext(),
  61.                         error.getMessage(), Toast.LENGTH_LONG).show();
  62.                 hideDialog();
  63.             }
  64.         }) {
  65.  
  66.             @Override
  67.             protected Map<String, String> getParams() {
  68.                 // Posting params to register url
  69.                 Map<String, String> params = new HashMap<String, String>();
  70.                 params.put("name", name);
  71.                 params.put("email", email);
  72.                 params.put("password", password);
  73.  
  74.                 return params;
  75.             }
  76.  
  77.         };
  78.  
  79.         // Adding request to request queue
  80.         AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement