Android - PROBLEMAS AL CORRER APLICACIÓN ANDROID EN EL CELULAR

 
Vista:

PROBLEMAS AL CORRER APLICACIÓN ANDROID EN EL CELULAR

Publicado por eder chacon (1 intervención) el 05/10/2018 21:26:59
hola. el problema es que cree una aplicacion sencilla para modificar una base de datos en localhost con ayuda phpmyadmin. cree unas funcione JSON para este proposito y todo funciona correctamente desde el emulador de android. el problema es que al simularlo desde el telefono, no modifica la base datos. es como si no conectara al url. y si le otorgue permission de internet en la aplicacion y uso directamente la ip de mi computador. GRACIAS de ante mano

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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("http://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("http://192.XXX.XX.XX/CONEXION_BASE/registro.php?nombres="+etNombres.getText().toString()+"&tel="+etTelefono.getText().toString());
 
            }
        });
 
 
 
 
    }
 
    private class CargarDatos extends AsyncTask<String, Void, String> {
        @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<String, Void, String> {
        @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);
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder