eclipse 错误:org.apache.http.conn.HttpHostConnectException:连接到 http://10.0.2.2:8080 被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23987635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error:org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
提问by Jorge T
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 api
错误:org.apache.http.conn.HttpHostConnectException:连接被http://10.0.2.2:8080
拒绝当Android使用php中的rest api,我连接到api
I 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.
我通过 USB 使用 eclipse 和我的 android 手机来运行我的应用程序。我的 api rest 是用 php 制作的,使用 xampp 并在端口 8080 上运行http://localhost:8080
。Xampp 适用于我的其他 Web 应用程序,但 Android 不起作用。
?-Also, my phone and my computer are not connected to the same network. -rest api can work in this case without internet?
?-另外,我的手机和我的电脑没有连接到同一个网络。-rest api 可以在没有互联网的情况下工作吗?
My code in android is this :
我在 android 中的代码是这样的:
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 .
注意:我读到http://10.0.2.2
只有在使用 android 模拟器时才有效,我使用通过 usb 连接的手机。
but nothing works , even <uses-permission android:name="android.permission.INTERNET"/>
I added in my manifest .
但没有任何效果,即使<uses-permission android:name="android.permission.INTERNET"/>
我在清单中添加了。
Sorry help me!!, PD: I do not speak English
对不起,帮帮我!!,PD:我不会说英语
采纳答案by vlio20
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 hereand replace it with localhost
.
如果您家中有 wifi 连接,请检查运行 xampp 的计算机的 IP 地址并将其替换为localhost
. 如果没有wifi连接并且您的计算机连接到调制解调器(没有路由器),那么只需在此处检查您的IP地址并将其替换为localhost
。
回答by seby598
I managed to fix this problem by writing my IP address from cmd -> ipconfig in the url:
我设法通过在 url 中从 cmd -> ipconfig 写入我的 IP 地址来解决这个问题:
HttpPost post = new HttpPost("http://192.168.x.x/rest/login/");
HttpPost post = new HttpPost(" http://192.168.xx/rest/login/");