Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MANUAL PARA CONSUMIR UN WEB SERVICE

1. Agregar una Java class con el nombre JSONParser

2. Dentro de ésta clase pegar el siguiente código (Nos servirá para la lectura o recorrido del
JSON)

public class JSONParser {


public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is,
Charset.forName("UTF-8")));
String jsonText = readAll(rd);
return new JSONObject(jsonText);
} finally {
is.close();
}
} private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}
3. Nos ubicamos en el MainActivity e Instanciamos las siguientes variables:

3.1. Para el diseño de la App:


private TextView textViewNames, textViewLastNames, textViewBirthday,
textViewMom, textViewDad, textViewOcupation;
private Button btnBuscar;
private EditText editxtCedula;
private CardView Card;
private Button btnNueva;

3.2. Para la lectura o recorrido del JSON


private String[] objetos=new String[6];
JSONObject jsonObjectText;

4. A continuación, abrimos el archivo activity_main y pegamos el siguiente código:

<?xml version="1.0" encoding="utf-8"?> app:layout_constraintBottom_toBottomOf="parent"


<androidx.constraintlayout.widget.ConstraintLayout app:layout_constraintEnd_toEndOf="parent"
xmlns:android="http://schemas.android.com/apk/res/and app:layout_constraintStart_toStartOf="parent"
roid" app:layout_constraintTop_toTopOf="parent"
xmlns:app="http://schemas.android.com/apk/res-auto" app:layout_constraintVertical_bias="0.37">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" <LinearLayout
android:layout_height="match_parent" android:layout_width="match_parent"
tools:context=".MainActivity"> android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
<EditText android:layout_marginVertical="10dp"
android:id="@+id/editTextCedula" android:background="#39867C"
android:layout_width="179dp" android:orientation="vertical">
android:layout_height="43dp"
android:ems="10" <LinearLayout
android:hint="Cédula" android:layout_width="match_parent"
android:inputType="number" android:layout_height="wrap_content"
android:maxLength="10" android:layout_marginHorizontal="10dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginVertical="0dp"
app:layout_constraintEnd_toEndOf="parent" android:orientation="vertical">
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.068" /> <TextView
android:id="@+id/textViewNombres"
<Button android:layout_width="match_parent"
android:id="@+id/buttonObtenerDatos" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:textAlignment="center"
android:layout_height="wrap_content" android:textSize="16sp"
android:layout_marginTop="64dp" android:textStyle="bold"
android:text="Obtener Datos" android:fontFamily="sans-serif-black"/>
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" <TextView
app:layout_constraintHorizontal_bias="0.498" android:id="@+id/textViewApellidos"
app:layout_constraintStart_toStartOf="parent" android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/editTextCe android:textAlignment="center"
dula" android:textSize="16sp"
app:layout_constraintVertical_bias="0.0" /> android:textStyle="bold"
android:fontFamily="sans-serif-black"/>
<androidx.cardview.widget.CardView </LinearLayout>
android:id="@+id/Card"
android:layout_width="341dp" <LinearLayout
android:layout_height="wrap_content" android:layout_width="match_parent"
android:visibility="gone" android:layout_height="wrap_content"
app:cardBackgroundColor="#0E6D64" android:layout_marginHorizontal="10dp"
android:layout_marginVertical="6dp" android:textStyle="bold" />
android:orientation="vertical">
<TextView
<TextView android:id="@+id/textViewOcupación"
android:id="@+id/textViewFechaNacimiento" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:textSize="14sp"
android:textSize="14sp" android:textStyle="bold" />
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/textViewMadre" <Button
android:layout_width="match_parent" android:id="@+id/New"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:textSize="14sp" android:layout_height="wrap_content"
android:textStyle="bold" /> android:layout_gravity="end"
android:text="Nueva Búsqueda" />
<TextView </LinearLayout>
android:id="@+id/textViewPadre" </androidx.cardview.widget.CardView>
android:layout_width="match_parent"
android:layout_height="wrap_content" </androidx.constraintlayout.widget.ConstraintLayout>
android:textSize="14sp"

5. Volvemos al MainActivity y dentro del onCreate:

5.1. Ponemos un título al Activity


setTitle("Consultar Datos");

5.2. Instanciamos la UI
editxtCedula = (EditText) findViewById(R.id.editTextCedula);
Card=(CardView) findViewById(R.id.Card);
btnNueva=(Button) findViewById(R.id.New);
btnBuscar = (Button) findViewById(R.id.buttonObtenerDatos);

5.3. Creamos la función setOnClickListener del btnBuscar


btnBuscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editxtCedula.getText().toString().length()>0 &&
editxtCedula.getText().toString().length()==10){
String url =
"http://si.secap.gob.ec/sisecap/servicioObtieneDatosRegistroCivil.php?num_doc="+editxtCedula.getText(
).toString();
new AsyncTaskExample().execute(url);
}
else
Toast.makeText(getApplicationContext(),"Debe ingresar una Cédula de 10
dígitos",Toast.LENGTH_LONG).show();
}
});

5.4. Creamos la función setOnClickListener del btnNueva


btnNueva.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Card.setVisibility(view.GONE);
editxtCedula.setText("");
editxtCedula.setVisibility(View.VISIBLE);
btnBuscar.setVisibility(View.VISIBLE);
}
});

6. Por último agregamos el método AsyncTaskExample, el cual nos va a permitir obtener


los datos del JSON y guardarlos en los textView’s anteriormente instanciados:
public class AsyncTaskExample extends AsyncTask<String, String, String[]> {

@Override
protected void onPreExecute() {
}

@Override
protected String[] doInBackground(String... url) {
try {
jsonObjectText = JSONParser.readJsonFromUrl(url[0]);

objetos[0]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(1);
objetos[1]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(0);
objetos[2]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(3);
objetos[3]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(6);
objetos[4]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(7);
objetos[5]=jsonObjectText.getJSONArray("rows").getJSONObject(0).getJSONArray("cell").g
etString(13);

} catch (IOException | JSONException e) {


e.printStackTrace();
}

return objetos;
}

@Override
protected void onPostExecute(String[] stringFromDoInBackground) {
if (!stringFromDoInBackground[0].contains("ul")){
Card.setVisibility(View.VISIBLE);
editxtCedula.setVisibility(View.GONE);
btnBuscar.setVisibility(View.GONE);

textViewNames = (TextView) findViewById(R.id.textViewNombres);


textViewLastNames = (TextView) findViewById(R.id.textViewApellidos);
textViewBirthday = (TextView) findViewById(R.id.textViewFechaNacimiento);
textViewMom = (TextView) findViewById(R.id.textViewMadre);
textViewDad = (TextView) findViewById(R.id.textViewPadre);
textViewOcupation = (TextView) findViewById(R.id.textViewOcupación);

textViewNames.setText(stringFromDoInBackground[0]);
textViewLastNames.setText(stringFromDoInBackground[1]);
textViewBirthday.setText("Fecha de Nacimiento:
"+stringFromDoInBackground[2]);
textViewMom.setText("Madre: "+stringFromDoInBackground[3]);
textViewDad.setText("Padre: "+stringFromDoInBackground[4]);
textViewOcupation.setText("Ocupación: "+stringFromDoInBackground[5]);
}else{
Card.setVisibility(View.INVISIBLE);
editxtCedula.setText("");
editxtCedula.setVisibility(View.VISIBLE);
btnBuscar.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),"La cédula no
existe",Toast.LENGTH_LONG).show();
}
}
}

You might also like