Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. package com.secondworld.grabthesky;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.Activity;
  5. import android.graphics.drawable.ColorDrawable;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.android.volley.Request;
  15. import com.android.volley.Response;
  16. import com.android.volley.VolleyError;
  17. import com.android.volley.toolbox.JsonObjectRequest;
  18.  
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.util.HashMap;
  23. import java.util.Map;
  24.  
  25. public class RegisterActivity extends AppCompatActivity {
  26.  
  27. TextView tvregister;
  28. EditText etnombre,etemail,etpass;
  29. String TAG = "registro";
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_register);
  35.  
  36. tvregister = (TextView)findViewById(R.id.rRegistro);
  37.  
  38. etnombre = (EditText)findViewById(R.id.rUser);
  39. etemail = (EditText)findViewById(R.id.rEmail);
  40. etpass = (EditText)findViewById(R.id.rPass);
  41.  
  42. tvregister.setOnClickListener(l1);
  43. }
  44.  
  45. private View.OnClickListener l1 = new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. Toast.makeText(
  49. RegisterActivity.this,
  50. "Registrando...",
  51. Toast.LENGTH_LONG).show();
  52. registrarUsuario();
  53. }
  54. };
  55.  
  56. public void registrarUsuario() {
  57.  
  58. // Obtener valores actuales de los controles
  59. final String nombre = etnombre.getText().toString();
  60. final String email = etemail.getText().toString();
  61. final String pass = etpass.getText().toString();
  62.  
  63. HashMap<String, String> map = new HashMap<>();// Mapeo previo
  64.  
  65. map.put("nombre", nombre);
  66. map.put("email", email);
  67. map.put("pass", pass);
  68.  
  69. // Crear nuevo objeto Json basado en el mapa
  70. JSONObject jobject = new JSONObject(map);
  71.  
  72. // Depurando objeto Json...
  73. Log.d(TAG, jobject.toString());
  74.  
  75. // Actualizar datos en el servidor
  76. VolleySingleton.getInstance(RegisterActivity.this).addToRequestQueue(
  77. new JsonObjectRequest(
  78. Request.Method.POST,
  79. Constantes.REGISTRARSE,
  80. jobject,
  81. new Response.Listener<JSONObject>() {
  82. @Override
  83. public void onResponse(JSONObject response) {
  84. // Procesar la respuesta del servidor
  85. procesarRespuesta(response);
  86. }
  87. },
  88. new Response.ErrorListener() {
  89. @Override
  90. public void onErrorResponse(VolleyError error) {
  91. Log.d(TAG, "Error Volley: " + error.getMessage());
  92. }
  93. }
  94.  
  95. ) {
  96. @Override
  97. public Map<String, String> getHeaders() {
  98. Map<String, String> headers = new HashMap<String, String>();
  99. headers.put("Content-Type", "application/json; charset=utf-8");
  100. headers.put("Accept", "application/json");
  101. return headers;
  102. }
  103.  
  104. @Override
  105. public String getBodyContentType() {
  106. return "application/json; charset=utf-8" + getParamsEncoding();
  107. }
  108. }
  109. );
  110.  
  111. }
  112.  
  113. /**
  114. * Procesa la respuesta obtenida desde el sevidor
  115. *
  116. * @param response Objeto Json
  117. */
  118. private void procesarRespuesta(JSONObject response) {
  119.  
  120. try {
  121. // Obtener estado
  122. String estado = response.getString("estado");
  123. // Obtener mensaje
  124. String mensaje = response.getString("mensaje");
  125.  
  126. switch (estado) {
  127. case "1":
  128. // Mostrar mensaje
  129. Toast.makeText(
  130. RegisterActivity.this,
  131. mensaje,
  132. Toast.LENGTH_LONG).show();
  133. // Enviar código de éxito
  134. RegisterActivity.this.setResult(Activity.RESULT_OK);
  135. // Terminar actividad
  136. RegisterActivity.this.finish();
  137. break;
  138.  
  139. case "2":
  140. // Mostrar mensaje
  141. Toast.makeText(
  142. RegisterActivity.this,
  143. mensaje,
  144. Toast.LENGTH_LONG).show();
  145. // Enviar código de falla
  146. RegisterActivity.this.setResult(Activity.RESULT_CANCELED);
  147. // Terminar actividad
  148. RegisterActivity.this.finish();
  149. break;
  150. }
  151. } catch (JSONException e) {
  152. e.printStackTrace();
  153. }
  154.  
  155. }
  156. }
  157.  
  158. require 'User.php';
  159.  
  160.  
  161. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  162.  
  163. // Decodificando formato Json
  164. $body = json_decode(file_get_contents("php://input"), true);
  165.  
  166. //Clave codificacion
  167. $clave = "l1Oia0JMKZFrDcPBAl0Znw";
  168.  
  169. // Insertar usuario
  170. $retorno = Usuarios::insertUsuario(
  171. $body['nombre'],
  172. $body['email'],
  173. // $body['pass']
  174. md5($clave.$body['pass']
  175. );
  176.  
  177. if ($retorno) {
  178. // Código de éxito
  179. print json_encode(
  180. array('estado' => '1','mensaje' => 'Creación exitosa')
  181. );
  182. } else {
  183. // Código de error
  184. print json_encode(
  185. array('estado' => '2','mensaje' => 'Creación fallida')
  186. );
  187. }
  188. }
  189.  
  190. [{"estado":"Ok","mensaje":"Registro realizado. Gracias."}]
  191.  
  192. {"estado":"Ok","mensaje":"Registro realizado. Gracias."}
  193.  
  194. private void procesarRespuesta(JSONArray response) {
  195.  
  196. try {
  197. //Del JSONArray recibido se extrae el JSONObject, todo lo demás queda como ya lo tienes
  198. JSONObject jsonobject = response.getJSONObject(0);
  199.  
  200. // Obtener estado
  201. String estado = response.getString("estado");
  202. // Obtener mensaje
  203. String mensaje = response.getString("mensaje");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement