Buen dia, tengo este codigo funcionando sin problema pero quiero que una vez se validen los datos y se vea el Toast de acceso correcto se abra otr actividad, espero me puedan ayudar a complementar el código.
public class MainActivity extends AppCompatActivity {Button botonEnviar;
TextView TextoUsuario, TextoPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
botonEnviar = (Button) findViewById(R.id.button);
TextoUsuario = (EditText) findViewById(R.id.user);
TextoPassword = (EditText) findViewById(R.id.password);
botonEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new EnvioPOST().execute("http://naikino.com/pizzeria/php/ingresar.php");
}
});
}
private class EnvioPOST extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "No se puede recuperar la pagina web. URL puede ser incorrecto.";
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
}
private String downloadUrl(String myurl) throws IOException {
Log.i("URL", "" + myurl);
myurl = myurl.replace(" ", "%20"); /// Cambiamos espacios por %20
InputStream is = null;
String respuestaComoString = null;
// Mostrar solo los primeros 500 carapteres del contenido
// recuperado de la pagina web.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milisegundos */);
conn.setConnectTimeout(15000 /* milisegundos */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
JSONObject variablesMetodoPost = new JSONObject();
variablesMetodoPost.put("user", TextoUsuario.getText().toString());
variablesMetodoPost.put("password", TextoPassword.getText().toString());
Log.e("params", variablesMetodoPost.toString());
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(obtenerDatosStringAEnviar(variablesMetodoPost));
writer.flush();
writer.close();
os.close();
//nos conectamos enviando el metodo de conexion y las variables
//obtenemos una respuesta
conn.connect();
int response = conn.getResponseCode();
Log.d("respuesta", "la respuesta es: " + response);
is = conn.getInputStream();
//convertimos el flujo de entrada en texto
respuestaComoString = readIt(is, len);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//se asegura de que el InputStream se cierra despues de la aplicacion y
//es terminado de utilizar
if (is != null) {
is.close();
}
}
return respuestaComoString;
}
public String obtenerDatosStringAEnviar(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while (itr.hasNext()) {
String key = itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char [len];
reader.read(buffer);
return new String(buffer);
}
public void registro(View view) {
Intent i = new Intent(this, Main2Activity.class);
startActivity(i);
}