Android : can't connect android device to centos vm

on Saturday, April 18, 2015


I'm having problems connecting to a virtual machine from an android device to test an app.


I've been reading all the posts related to this error, but still can't solve it. I don't know much about networks.


I'm getting the error "java.net.UnknownHostException: Unable to resolve host "www.votatron.local": No address associated with hostname"


I put the www.votatron.local domain in my windows host file. I can correctly access the server from my PC browsers (chrome and firefox). I do a request with HTTP REQUESTER to my PHP script and I get the right response.


The problem comes when using the android device or the android emulator. I can't seem to access the domain from the app and from the device's browser.


I have the right permissions set in my manifest (INTERNET & ACCESS_NETWORK_STATE).


I have disconnected the LAN adapter, and both, the PC and the android device, connect to the same WIFI connection.


I'm stuck with this issue for 4 days now and can't seem to solve it. As i said I don't know much about networks.


By the way, I checked my chrome(pc) settings and It seems it's not conecting through a proxy.


Thanks in advance for any help.


This is the AsyncTask I'm using to connect:



private class CheckInAsyncTask extends AsyncTask<String, Integer, Boolean> {

@Override
protected Boolean doInBackground(String... params) {
boolean exito = false;
InputStream is = null;
int len = 1000;

// Gets the URL from the UI's text field.
String stringUrl = getResources().getString(R.string.url_login_php_script);
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
URL url;
HttpURLConnection conn;
try {
//url = new URL(stringUrl);
//url = new URL("http://172.16.100.5:9090/login_registro.php");
//url = new URL("http://172.16.100.5/login_registro.php");
//url = new URL("http://172.16.100.5:8080/login_registro.php");
url = new URL("http://www.votatron.local/login_registro.php");

String param="email=" + URLEncoder.encode(email, "UTF-8");

conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoOutput(true);

conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();

String response= "";

Log.e("ERROR -----------> ", "HOLAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAA");

// LA SIGUIENTE LINEA ESTÁ DANDO ERROR
Scanner inStream = new Scanner(conn.getInputStream(), conn.getContentEncoding());
while(inStream.hasNextLine()) {
response += (inStream.nextLine());
}

char[] respuestaArray = response.toCharArray();
int respuestaNum = Integer.parseInt(Character.toString(respuestaArray[respuestaArray.length-1]));

if(respuestaNum==1){
//lanzo la aplicacion
exito = true;
} else if (respuestaNum==0){
// repito el proceso
exito = false;
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

} else {
Toast.makeText(getApplicationContext(), "imposible realizar la conexion con el servicio", Toast.LENGTH_LONG).show();
exito = false;
}
return exito;
}

@Override
protected void onProgressUpdate(Integer... values) {
//super.onProgressUpdate(values);

}

@Override
protected void onPostExecute(Boolean aBoolean) {
//super.onPostExecute(aBoolean);
if(aBoolean){
// cierro esta actividad y abro la actividad principal
Intent entrarApp = new Intent().setClass(MainActivity.this,
com.votatron.votatron.Principal.class);
startActivity(entrarApp);
finish();
} else {
// repito el proceso
new CheckInAsyncTask().execute(email);
}
}
}

0 comments:

Post a Comment