0
I’m creating an app in Android Studio that will send information to a web service that will insert the data in the database.
However when I press to enter the data, the program returns this error:
Failed to connect to server: java.net.UnknownHostException: Unable to resolve host "play.googleapis.com": No address associated with hostname
Mainactivity:
public class MainActivity extends AppCompatActivity {
String inserturl = "http://localhost/inserirDados.php";
EditText nome;
EditText endereco;
Button inserir;
Button selecionar;
RequestQueue rq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nome = (EditText) findViewById(R.id.txtNome);
endereco = (EditText) findViewById(R.id.txtEndereco);
inserir = (Button) findViewById(R.id.btEnviarDados);
selecionar = (Button) findViewById(R.id.btMostrarDados);
rq = Volley.newRequestQueue(getApplicationContext());
inserir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inserir();
}
});
}
public void inserir(){
StringRequest request = new StringRequest(Request.Method.POST, inserturl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parametros = new HashMap<String, String>();
parametros.put("nome", nome.getText().toString());
parametros.put("enredeco", endereco.getText().toString());
return parametros;
}
};
rq.add(request);
}
}
Remembering that my Androidmanifest.xml has the following permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Could someone help me understand what I need to do to insert an app’s data into an external database ?
localhost points to the device itself, ideally you point to the domain or ip of your dev machine and when you go up to production you place the external URL where the webservice is
– George Moura
@Georgemoura I have already accomplished this process and returns the same error.
– Gabriel Henrique
Already tried to change
rq = Volley.newRequestQueue(getApplicationContext());
byrq = Volley.newRequestQueue(this);
I used this example as a base: https://developer.android.com/training/volley/simple.html#simple– George Moura
I have performed this test, but there is no change and does not insert in the database.
– Gabriel Henrique
I’ll see if I can replicate that mistake
– George Moura
@Georgemoura thanks for the help George, tonight I will see with a Java/Android programmer how to do to solve the problem. If so, put the solution as an answer. Thank you.
– Gabriel Henrique
how does it return a host error "play.googleapis.com" if the url is pointing to "insert.php"? It may be that this error comes from another party. Something else
localhost
always points to the own machine, in the case of the android always to the own cellular then with that url will never arrive in the service, will need the ip of the machine, has also such magic ip10.0.2.2
which in theory points to the localhost of the machine that is connected via usb.– Neuber Oliveira