Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginClass {
- public static class ValidarSesion extends AsyncTask<String,String,JSONObject>
- {
- @Override
- protected JSONObject doInBackground(String... parametros) {
- String datos = parametros[0];
- String JsonResponse = "";
- HttpURLConnection urlConnection = null;
- BufferedReader reader = null;
- OutputStream os = null;
- //-----------------------------
- try {
- URL url = new URL("http://"192.168.8.1"/webappcesfam/web/index.php?r=paciente/login-paciente"); //Inicia la conexion al webservice.
- try {
- urlConnection = (HttpURLConnection) url.openConnection();
- } catch (IOException e) {
- e.printStackTrace();
- }
- urlConnection.setDoOutput(true);
- urlConnection.setRequestMethod("POST");
- urlConnection.setRequestProperty("Content-Type", "application/json");
- urlConnection.setRequestProperty("Accept", "application/json");
- urlConnection.connect();
- os = new BufferedOutputStream(urlConnection.getOutputStream());
- os.write(datos.toString().getBytes()); //empieza a enviar los datos al webservice
- os.flush();
- InputStream inputStream = urlConnection.getInputStream();
- StringBuffer buffer = new StringBuffer();
- reader = new BufferedReader(new InputStreamReader(inputStream));
- String inputLine = "";
- while ((inputLine = reader.readLine()) != null)
- {
- buffer.append(inputLine);
- }
- JsonResponse = buffer.toString();
- JSONObject resultadoJSON = null;
- try {
- resultadoJSON = new JSONObject(JsonResponse); //obtiene la respuesta desde el webservice en formato JSON, Siempre sera un par de datos : ID y los datos
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return resultadoJSON;
- } catch (IOException e) {
- Log.d("Session", "Error1: " + e.getMessage());
- e.printStackTrace();
- } finally {
- if (urlConnection != null) {
- urlConnection.disconnect();
- }
- if (reader != null) {
- try {
- reader.close();
- } catch (final IOException e) {
- Log.d("Session", "Error3: " + e.getMessage());
- }
- }
- }
- return null;
- }
- protected void onPostExecute(JSONObject respuestaOdata) //envias la respuesta del webservice a la clase principal en mi caso
- {
- Login l=(Login)ControllerActivity.actividadActual;
- l.recibirMensajeValdiacion(respuestaOdata);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement