Вы находитесь на странице: 1из 2

{

Button btnconsultar,btnGuardar;
EditText etDni ,etNombres, etApellidos;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
package yampol.solano.appregistrocliente;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity


btnconsultar = (Button) findViewById(R.id.btnConsultar);
btnGuardar = (Button) findViewById( R.id.btnGuardar);
etDni =(EditText)findViewById(R.id.etCodigo);
etNombres =(EditText)findViewById(R.id.etNombres);
etApellidos =(EditText)findViewById(R.id.etApellidos);

btnconsultar.setOnClickListener((v)→ {
new
ConsultarDatos().execute("http://10.0.2.2:8080/Restaurante/consulta.php?
dni="+etDni.getText().toString());
});

btnGuardar.setOnClickListener((v)→{
new
CargarDatos().execute("http//10.0.2.2:8080/Restaurante/registro.php?
dni="+etDni.getText().toString()+
"nombres="+etNombres.getText().toString()
+etApellidos.getText().toString());
});

private class CargarDatos extends AsyncTask<String, Void, String > {


@Override
protected String doInBackground(String... urls){
try{
return downloadUrl(urls[0]);
}catch(IOException e){
return "unable to retrieve web page.URl may be inavalid.";
}
}
@Override
protected void onPostExecute(String result){
Toast.makeText(getApplicationContext(),"se almacenaron los datos
correctamente", Toast.LENGTH_LONG).show();
}
}

private class ConsultarDatos extends AsyncTask<String,Void,String> {


@Override
protected String doInBackground(String... urls) {
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "unable to retrieve web page.URl may be inavalid.";
}
}}
@Override
protected void OnPostExecute(String result){
JSONArray ja=null;
try {
ja=new JSONArray(result);
etDni.setText(ja.getString(1));
etNombres.setText(ja.getString(2));
etApellidos.setText(ja.getString(3));

} catch (JSONException e) {
e.printStackTrace();

}}}

private String downloadUrl(String myurl)throws IOException {


Log.i("URL",""+myurl);
myurl = myurl.replace("","%20");
InputStream is = null;

int len = 500;

try{
URL url=new URL(myurl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000);
conn.setconnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);

conn.connect();
int response =conn.getResponseCode();
Log.d("respuesta","the response is:"+response);
is=conn.getInputStream();

String contentAsAtring = readIt(is,len);


return contentAsString;
}
finally{
if (is !=null){
is.close();
} } }

public String readIt(InputStream stream ,int len)throws IOException ,


UnsupportedEncodEncodingException{
Reader reader=null;
reader=new InputStreamReader(stream,"UTF-8");
char[] buffer = new char[len] ;
reader.read(buffer);
return new String (buffer);
}
}

Вам также может понравиться