dimanche 19 avril 2015

can't connect android device to centos vm

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://ift.tt/1b8MBzf");
//url = new URL("http://ift.tt/1b8MBzh");
//url = new URL("http://ift.tt/1b8MBzl");
url = new URL("http://ift.tt/1EXOZox");

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);
}
}
}


EDIT


I have left the virtual machine aside and have created the webservice in a free hosting website. Now it seems to connect to it, but I'm having another problem.


I get the following exception:



04-19 13:27:15.506 21187-21288/com.votatron.votatron E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.votatron.votatron, PID: 21187
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: charsetName == null
at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
at java.util.Scanner.<init>(Scanner.java:212)
at com.votatron.votatron.MainActivity$CheckInAsyncTask.doInBackground(MainActivity.java:271)
at com.votatron.votatron.MainActivity$CheckInAsyncTask.doInBackground(MainActivity.java:228)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)


I guess the problem must be in this section of the code:



Scanner inStream = new Scanner(conn.getInputStream(), conn.getContentEncoding());
while(inStream.hasNextLine()) {
response += (inStream.nextLine());
}

Aucun commentaire:

Enregistrer un commentaire