- Posts del Foro: 1
5 oct. 2018 21:42:44 vía Web
5 oct. 2018 21:42:44 vía Web
hola. el problema es que cree una aplicación sencilla para modificar una base de datos en localhost con ayuda phpmyadmin. cree unas funcione JSON para este propósito y todo funciona correctamente desde el emulador de android. el problema es que al simularlo desde el teléfono, no modifica la base datos. es como si no conectara al url. y si le otorgue permission de internet en la aplicación y uso directamente la ip de mi computador. GRACIAS de ante mano
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Button btnconsultar, btnGuardar;
EditText etId, etNombres, etTelefono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnconsultar = findViewById(R.id.btnConsultar);
btnGuardar = findViewById(R.id.btnGuardar);
etId = findViewById(R.id.etId);
etNombres = findViewById(R.id.etNombres);
etTelefono = findViewById(R.id.etTelefono);
btnconsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ConsultarDatos().execute("htp://192.XXX.XX.XXX/CONEXION_BASE/consulta.php?id="+etId.getText().toString());
}
});
btnGuardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CargarDatos().execute("htp://192.XXX.XX.XX/CONEXION_BASE/registro.php?nombres="+etNombres.getText().toString()+"&tel="+etTelefono.getText().toString());
}
});
}
private class CargarDatos extends AsyncTask {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "Se almacenaron los datos correctamente", Toast.LENGTH_LONG).show();
}
}
private class ConsultarDatos extends AsyncTask {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
JSONArray ja = null;
try {
ja = new JSONArray(result);
etNombres.setText(ja.getString(1));
etTelefono.setText(ja.getString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private String downloadUrl(String myurl) throws IOException {
Log.i("URL",""+myurl);
myurl = myurl.replace(" ","%20");
InputStream is = null;
//// Only display the first 500 characters of the retrieved
//// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds /);
conn.setConnectTimeout(15000 / milliseconds */);
conn.setRequestMethod("REQUEST");
conn.setDoInput(true);
//// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("respuesta", "The response is: " + response);
is = conn.getInputStream();
//// Convert the InputStream into a string
return readIt(is, len);
//// Makes sure that the InputStream is closed after the app is
//// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
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);
Contenido editorial recomendado
Con su consentimiento, aquí se cargan contenidos externos.
Al hacer clic en el botón anterior, acepta que se le muestren contenidos externos. En el proceso pueden transmitirse datos personales a terceros proveedores. Encontrará más información al respecto en nuestro Política de privacidad.