Connection to http://localhost:8080 refused in Android -
i trying consume rest service in android , getting following error:
org.apache.http.conn.httphostconnectexception: connection http://localhost:8080 refused
i have introduced permission: uses-permission android:name="android.permission.internet"
running code in emulator , works when try run in smartphone fails.
pd: when run in emulator use http://10.0.2.2:8080.
public boolean checkconnection(){ string res = ""; boolean serverup = false; try { httpclient client = new defaulthttpclient(); //httpget request = new httpget("http://10.0.2.2:8080/prueba_conexion"); httpget request = new httpget("http://localhost:8080/prueba_conexion"); httpresponse response = client.execute(request); bufferedreader rd = new bufferedreader(new inputstreamreader( response.getentity().getcontent())); string linea = ""; while ((linea = rd.readline()) != null) { res += linea; } serverup = boolean.parseboolean(res); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return serverup; }
localhost doesnt work on phone, because refers phone itself.
you need local ip address , use instead ie. 192.168.something.something usually.
Comments
Post a Comment