Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
  2.  
  3. require_once 'include/DB_Functions.php';
  4. $db = new DB_Functions();
  5.  
  6. // json response array
  7. $response = array("error" => FALSE);
  8.  
  9. if (isset($_POST['email']) && isset($_POST['password'])) {
  10.  
  11. // receiving the post params
  12. $email = $_POST['email'];
  13. $password = $_POST['password'];
  14.  
  15. // get the user by email and password
  16. $user = $db->getUserByEmailAndPassword($email, $password);
  17.  
  18. if ($user != false) {
  19. // use is found
  20. $response["error"] = FALSE;
  21. $response["uid"] = $user["unique_id"];
  22. $response["user"]["name"] = $user["name"];
  23. $response["user"]["cognome"] = $user["cognome"];
  24. $response["user"]["email"] = $user["email"];
  25. $response["user"]["email2"] = $user["email2"];
  26. $response["user"]["numero_appartamento"] = $user["numero_appartamento"];
  27. $response["user"]["nome_edificio"] = $user["nome_edificio"];
  28. $response["user"]["zona_metropolitana"] = $user["zona_metropolitana"];
  29. $response["user"]["created_at"] = $user["created_at"];
  30. $response["user"]["updated_at"] = $user["updated_at"];
  31. echo json_encode($response);
  32. } else {
  33. // user is not found with the credentials
  34. $response["error"] = TRUE;
  35. $response["error_msg"] = "Login credentials are wrong. Please try again!";
  36. echo json_encode($response);
  37. }
  38. } else {
  39. // required post params is missing
  40. $response["error"] = TRUE;
  41. $response["error_msg"] = "Required parameters email or password is missing!";
  42. echo json_encode($response);
  43. }
  44. ?>
  45.  
  46. private void checkLogin(final String email, final String password) {
  47. // Tag used to cancel the request
  48. String tag_string_req = "req_login";
  49.  
  50. pDialog.setMessage("Logging in ...");
  51. showDialog();
  52.  
  53. StringRequest strReq = new StringRequest(Method.POST,
  54. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  55.  
  56. @Override
  57. public void onResponse(String response) {
  58. Log.d(TAG, "Login Response: " + response.toString());
  59. hideDialog();
  60.  
  61. try {
  62. JSONObject jObj = new JSONObject(response);
  63. boolean error = jObj.getBoolean("error");
  64.  
  65. // Check for error node in json
  66. if (!error) {
  67. // user successfully logged in
  68. // Create login session
  69. session.setLogin(true);
  70.  
  71. // Now store the user in SQLite
  72. String uid = jObj.getString("uid");
  73.  
  74. JSONObject user = jObj.getJSONObject("user");
  75. String name = user.getString("name");
  76. String cognome = user.getString("cognome");
  77.  
  78. String email = user.getString("email");
  79. String email2 = user.getString("email2");
  80. String numero_appartamento = user.getString("numero_appartamento");
  81. String nome_edificio = user.getString("nome_edificio");
  82. String zona_metropolitana = user.getString("zona_metropolitana");
  83.  
  84.  
  85.  
  86. String created_at = user
  87. .getString("created_at");
  88.  
  89. // Inserting row in users table
  90. db.addUser(name,cognome, email,email2,numero_appartamento,nome_edificio,zona_metropolitana,uid, created_at);
  91.  
  92. // Launch main activity
  93. Intent intent = new Intent(LoginActivity.this,
  94. // MainActivity.class);
  95. ScrollableTabsActivity.class);
  96.  
  97. startActivity(intent);
  98. finish();
  99. } else {
  100. // Error in login. Get the error message
  101. String errorMsg = jObj.getString("error_msg");
  102. Toast.makeText(getApplicationContext(),
  103. errorMsg, Toast.LENGTH_LONG).show();
  104. }
  105. } catch (JSONException e) {
  106. // JSON error
  107. e.printStackTrace();
  108. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  109. }
  110.  
  111. }
  112. }, new Response.ErrorListener() {
  113.  
  114. @Override
  115. public void onErrorResponse(VolleyError error) {
  116. Log.e(TAG, "Login Error: " + error.getMessage());
  117. Toast.makeText(getApplicationContext(),
  118. error.getMessage(), Toast.LENGTH_LONG).show();
  119. hideDialog();
  120. }
  121. }) {
  122.  
  123. @Override
  124. protected Map<String, String> getParams() {
  125. // Posting parameters to login url
  126. Map<String, String> params = new HashMap<String, String>();
  127. params.put("email", email);
  128. params.put("password", password);
  129. System.out.println(params);
  130. return params;
  131. }
  132.  
  133. };
  134.  
  135. // Adding request to request queue
  136. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  137. }
  138.  
  139. JSONObject jObj = new JSONObject(response);
  140.  
  141. AppConfig.URL_LOGIN, new Response.Listener<String>()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement