This is the script that was created to generate the requests to the server. My question is, if you can send the requests in json format and receive the answer in the same way.
This is the script code.
package com.paradox.house.other
import android.app.Activity
import android.app.AlertDialog
import android.app.Application
import android.content.Intent
import android.preference.PreferenceManager
import android.util.Log
import com.paradox.house.*
import com.paradox.house.model.RegistroPaciente
import khttp.responses.Response
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.json.JSONArray
import org.json.JSONObject
import khttp.delete as httpDelete
object Services {
val rutaBASEServidor = Variables.url_servicios
fun registrarPaciente(paciente: RegistroPaciente, context: Application, activity: Activity) {
var success = false;
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "paciente/registrar",
data = mapOf("nombre" to paciente.nombre, "apellidos" to paciente.apellidos,
"curp" to paciente.curp, "sexo" to paciente.sexo, "edad" to paciente.edad,
"domicilio" to paciente.domicilio, "ocupacion" to paciente.ocupacion,
"peso" to paciente.peso, "embarazada" to paciente.embarazada,
"unidadMedica" to paciente.unidadMedica, "diagnostico" to paciente.diagnostico,
"fechaDiagnostico" to paciente.fechaDiagnostico,
"noSeguroPopular" to paciente.noSeguroPopular,
"jurisdiccion" to paciente.jurisdiccion, "infeccion" to paciente.infeccion,
"deteccion" to paciente.deteccion, "clasificacion" to paciente.clasificacion,
"confirmacion" to paciente.confirmacion, "lote" to paciente.lote,
"esquema" to paciente.esquema, "causaTermino" to paciente.causaTermino,
"idLocalidad" to paciente.idLocalidad, "idMedico" to paciente.idMedico)
)
val respuesta:JSONObject = response.jsonObject
val mensaje = respuesta.get("success") as Boolean
val idPaciente = respuesta.get("idPaciente") as Int
success = mensaje;
uiThread {
if(success) {
val intent = Intent(context, RegistrarVisitaActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("idPaciente", idPaciente.toString())
context.startActivity(intent)
activity.finish()
} else {
val builder = AlertDialog.Builder(activity)
builder.setTitle("Error al registrar paciente")
builder.setMessage("El paciente ya se encuentra registrado")
val dialog:AlertDialog = builder.create()
dialog.show()
}
}
}
}
fun cerrarSesion(context: Application, activity: Activity){
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
val editor = sharedPreferences.edit()
editor.putString("username", "")
editor.putString("password", "")
editor.commit()
val intent = Intent(context, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
activity.finish()
}
fun ingresar(email: String, password: String, context: Application, activity: Activity) {
var apellido_medico:String
var nombre_medico:String
var nombreDoctor = ""
var idMedico = 0
var success = false
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "login",
data = mapOf("usuario" to email.toString(), "password" to password.toString()))
val respuesta: JSONObject = response.jsonObject
Log.d("Respuesta", respuesta.toString())
val mensaje = respuesta.get("success") as Boolean
var admin = 0
var usuario = ""
if (mensaje) {
admin = respuesta.get("admin") as Int
if (admin == 0) {
apellido_medico = respuesta.get("medico_apellidos") as String
nombre_medico = respuesta.get("medico_nombre") as String
nombreDoctor = nombre_medico + " " + apellido_medico
idMedico = respuesta.get("idMedico") as Int
}
usuario = respuesta.get("username") as String
success = mensaje
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
val editor = sharedPreferences.edit()
editor.putString("username", email)
editor.putString("password", password)
editor.putInt("idMedico", idMedico)
editor.commit()
} else {
error_respuesta = respuesta.get("error") as String
}
uiThread {
if (success) {
if (admin == 1) {
val intent = Intent(context, PrincipalAdminActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("usuario_admin", usuario)
context.startActivity(intent)
activity.finish()
} else {
val intent = Intent(context, PrincipalMedicoActivity::class.java)
intent.putExtra("nombre_doctor", nombreDoctor)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
activity.finish()
}
} else {
val builder = AlertDialog.Builder(activity)
builder.setTitle("Error al iniciar sesión")
builder.setMessage(error_respuesta)
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
}
fun ingresar_inicio(email: String, password: String, context: Application, activity: Activity) {
var apellido_medico:String
var nombre_medico:String
var nombreDoctor = ""
var idMedico = 0
var success = false
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "login",
json = mapOf("usuario" to email.toString(), "password" to password.toString()))
val respuesta: JSONObject = response.jsonObject
Log.d("Respuesta",respuesta.toString())
val mensaje = respuesta.get("success") as Boolean
var usuario= ""
var admin = 0
if(mensaje) {
admin = respuesta.get("admin") as Int
if(admin == 0){
apellido_medico = respuesta.get("medico_apellidos") as String
nombre_medico = respuesta.get("medico_nombre") as String
nombreDoctor = nombre_medico + " " +apellido_medico
idMedico = respuesta.get("idMedico") as Int
}
usuario = respuesta.get("username") as String
success = mensaje
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
val editor = sharedPreferences.edit()
editor.putString("username", email)
editor.putString("password", password)
editor.putInt("idMedico",idMedico)
editor.commit()
}else{
error_respuesta = respuesta.get("error") as String
}
uiThread {
if (success) {
if(admin == 1){
val intent = Intent(context, PrincipalAdminActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("usuario_admin",usuario)
context.startActivity(intent)
activity.finish()
}else{
val intent = Intent(context, PrincipalMedicoActivity::class.java)
intent.putExtra("nombre_doctor",nombreDoctor)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
activity.finish()
}
}
}
}
}
fun registrarMedico(nombre: String, apellidos: String, unidadMedica: String,
correoElectronico: String, idMunicipio: String, password: String,
context: Application, activity: Activity) {
var success = false
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "medico/registro",
data = mapOf("nombre" to nombre, "apellidos" to apellidos, "unidadMedica"
to unidadMedica, "correoElectronico" to correoElectronico,
"idMunicipio" to idMunicipio, "password" to password))
val respuesta: JSONObject = response.jsonObject
val mensaje = respuesta.get("success") as Boolean
if(mensaje) {
success = mensaje
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
val editor = sharedPreferences.edit()
editor.putString("username", correoElectronico)
editor.putString("password", password)
editor.commit()
}
uiThread {
if (success) {
var nombreDoctor = nombre + " " + apellidos
val intent = Intent(context, PrincipalMedicoActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("nombre_doctor",nombreDoctor)
context.startActivity(intent)
activity.finish()
} else {
val builder = AlertDialog.Builder(activity)
builder.setTitle("Error al registrarse")
builder.setMessage(error_respuesta)
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
}
fun getPacientes( context: Application,
activity: Activity){
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
val idMedico = sharedPreferences.getInt("idMedico", 0)
Log.d("idMedico",idMedico.toString())
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "paciente/get",
data = mapOf("idMedico" to idMedico))
val respuesta: JSONArray = response.jsonArray
Log.d("respuesta",respuesta.toString())
uiThread {
}
}
}
fun registrarTratamiento(fecha: String, peso: String, medicamento: String, renuente: String,
reingreso: String, reaccionesAdversas: String, idPaciente: String,
context: Application, activity: Activity) {
var success = false
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "tratamiento/registrar",
data = mapOf("fecha" to fecha, "peso" to peso, "medicamento" to medicamento,
"renuente" to renuente, "reingreso" to reingreso,
"reaccionesAdversas" to reaccionesAdversas, "idPaciente" to idPaciente))
val respuesta: JSONObject = response.jsonObject
val mensaje = respuesta.get("success") as Boolean
if (mensaje) {
success = mensaje
}
uiThread {
if (success) {
val intent = Intent(context, VerInformacionPaciente::class.java)
intent.putExtra("idPaciente", idPaciente)
context.startActivity(intent)
activity.finish()
} else {
val builder = AlertDialog.Builder(activity)
builder.setTitle("Error al registrar tratamiento")
builder.setMessage(error_respuesta)
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
}
fun actualizarCausaTermino(idPaciente: String, causaTermino: String, context: Application,
activity: Activity) {
var success = false
var error_respuesta = ""
doAsync {
val response: Response = khttp.post(
url = rutaBASEServidor + "paciente/actualizar",
data = mapOf("idPaciente" to idPaciente, "causaTermino" to causaTermino))
val respuesta: JSONObject = response.jsonObject
val mensaje = respuesta.get("success") as Boolean
if(mensaje) {
success = mensaje
}
uiThread {
if (!success) {
val builder = AlertDialog.Builder(activity)
builder.setTitle("Error al actualizar causa término")
builder.setMessage(error_respuesta)
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
}
}