Error:org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
Solution 1
First you have an error:
HttpPost post = new HttpPost ( " http:/127.0.0.1:8080/rest/login/ ");
should be:
HttpPost post = new HttpPost ( " http://127.0.0.1:8080/rest/login/ ");
Next if the phone is not connected to your coputer network it wont ever connect to your local server aka your computer.
If you have a wifi connection at your home check the ip address of the computer which runs xampp and replace it with localhost
. If there is no wifi connection and your computer if connected to your modem (without router) then just check your ip address here and replace it with localhost
.
Solution 2
I managed to fix this problem by writing my IP address from cmd -> ipconfig in the url:
HttpPost post = new HttpPost("http://192.168.x.x/rest/login/");
Jorge T
Updated on June 13, 2022Comments
-
Jorge T almost 2 years
Error:org.apache.http.conn.HttpHostConnectException: Connection to
http://10.0.2.2:8080
refused When Android consumes rest api in php, and I connect to the apiI use eclipse and my android phone via usb to run my app . My api rest is made in php and use xampp and it run on port 8080
http://localhost:8080
. Xampp works with my other web application , but Android does not work.-Also, my phone and my computer are not connected to the same network. -rest api can work in this case without internet?
My code in android is this :
private class TareaWSInsertar extends AsyncTask<String,Integer,Boolean> { //agregado user_id, user_fullname, user_email private int user_id; private String user_fullname; private String user_email; protected Boolean doInBackground(String... params) { boolean resul = true; HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2:8080/rest/login/"); post.setHeader("content-type", "application/json"); try { //Construimos el objeto cliente en formato JSON JSONObject dato = new JSONObject(); dato.put("email", params[0]); dato.put("pwd", params[1]); StringEntity entity = new StringEntity(dato.toString()); post.setEntity(entity); HttpResponse resp = httpClient.execute(post); String respStr = EntityUtils.toString(resp.getEntity()); JSONObject respJSON = new JSONObject(respStr); user_id = respJSON.getInt("user_id"); user_fullname = respJSON.getString(user_fullname); user_email = respJSON.getString("user_email"); if(!respStr.equals("true")) resul = false; } catch(Exception ex) { Log.e("ServicioRest","Error!", ex); resul = false; } return resul; } protected void onPostExecute(Boolean result) { if (result) { Toast.makeText(getApplicationContext(), "" + user_id+ "-" + user_fullname + "-" + user_email, Toast.LENGTH_LONG).show(); } } }
I have tried different way :
http://10.0.2.2:8080/rest/login http://localhost:8080/rest/login http:/127.0.0.1:8080/rest/login
Note: I read that
http://10.0.2.2
only works when the android emulator is used, I use my phone connected via usb .but nothing works , even
<uses-permission android:name="android.permission.INTERNET"/>
I added in my manifest .Sorry help me!!, PD: I do not speak English