Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class LoginClass {
  2.     public static class ValidarSesion extends AsyncTask<String,String,JSONObject>
  3.     {
  4.         @Override
  5.         protected JSONObject doInBackground(String... parametros) {
  6.  
  7.             String datos = parametros[0];
  8.  
  9.  
  10.             String JsonResponse = "";
  11.             HttpURLConnection urlConnection = null;
  12.             BufferedReader reader = null;
  13.             OutputStream os = null;
  14.             //-----------------------------
  15.  
  16.             try {
  17.  
  18.                 URL url = new URL("http://"192.168.8.1"/webappcesfam/web/index.php?r=paciente/login-paciente"); //Inicia la conexion al webservice.
  19.                 try {
  20.                     urlConnection = (HttpURLConnection) url.openConnection();
  21.                 } catch (IOException e) {
  22.                     e.printStackTrace();
  23.                 }
  24.                 urlConnection.setDoOutput(true);
  25.                 urlConnection.setRequestMethod("POST");
  26.                 urlConnection.setRequestProperty("Content-Type", "application/json");
  27.                 urlConnection.setRequestProperty("Accept", "application/json");
  28.                 urlConnection.connect();
  29.  
  30.                 os = new BufferedOutputStream(urlConnection.getOutputStream());
  31.  
  32.  
  33.                 os.write(datos.toString().getBytes()); //empieza a enviar los datos al webservice
  34.                 os.flush();
  35.  
  36.  
  37.                 InputStream inputStream = urlConnection.getInputStream();
  38.  
  39.  
  40.                 StringBuffer buffer = new StringBuffer();
  41.                 reader = new BufferedReader(new InputStreamReader(inputStream));
  42.                 String inputLine = "";
  43.                 while ((inputLine = reader.readLine()) != null)
  44.                 {
  45.                     buffer.append(inputLine);
  46.                 }
  47.  
  48.                 JsonResponse = buffer.toString();
  49.  
  50.  
  51.                 JSONObject resultadoJSON = null;
  52.                 try {
  53.                     resultadoJSON = new JSONObject(JsonResponse); //obtiene la respuesta desde el webservice en formato JSON, Siempre sera un par de datos : ID y los datos
  54.                 } catch (JSONException e) {
  55.                     e.printStackTrace();
  56.                 }
  57.  
  58.  
  59.                 return resultadoJSON;
  60.  
  61.  
  62.             } catch (IOException e) {
  63.                 Log.d("Session", "Error1: " + e.getMessage());
  64.                 e.printStackTrace();
  65.             } finally {
  66.                 if (urlConnection != null) {
  67.                     urlConnection.disconnect();
  68.                 }
  69.                 if (reader != null) {
  70.                     try {
  71.                         reader.close();
  72.                     } catch (final IOException e) {
  73.                         Log.d("Session", "Error3: " + e.getMessage());
  74.                     }
  75.                 }
  76.             }
  77.             return null;
  78.         }
  79.  
  80.         protected void onPostExecute(JSONObject respuestaOdata) //envias la respuesta del webservice a la clase principal en mi caso
  81.         {
  82.             Login l=(Login)ControllerActivity.actividadActual;
  83.             l.recibirMensajeValdiacion(respuestaOdata);
  84.  
  85.  
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement