java 为什么我收到 HttpHostConnectException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10676861/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:03:37  来源:igfitidea点击:

Why I am getting the HttpHostConnectException

javaandroidexception

提问by Muhammad Salman Farooq

I am using the following code in android virtual Machine

我在android虚拟机中使用以下代码

 try{
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://10.0.2.2/ReadingFromServer.php");
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       HttpResponse response = httpclient.execute(httppost);
       HttpEntity entity = response.getEntity();
       is = entity.getContent();
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection"+e.toString());
      }

I am getting the HttpHostConnectException. Dont know why? I have changed the address in the url from 127.0.0.1 to 10.0.2.2 but still getting that exception. I have wamp server installed in my computer and the file "ReadingFromServer.php" is placed in "www" folder.

我收到了 HttpHostConnectException。不知道为什么?我已将 url 中的地址从 127.0.0.1 更改为 10.0.2.2,但仍然收到该异常。我的电脑上安装了 wamp 服务器,文件“ReadingFromServer.php”放在“www”文件夹中。

Here is the complete stack trace

这是完整的堆栈跟踪

05-20 20:40:32.218: W/System.err(681): org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused
05-20 20:40:32.248: W/System.err(681):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-20 20:40:32.258: W/System.err(681):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-20 20:40:32.268: W/System.err(681):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-20 20:40:32.278: W/System.err(681):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-20 20:40:32.288: W/System.err(681):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-20 20:40:32.298: W/System.err(681):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-20 20:40:32.308: W/System.err(681):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-20 20:40:32.318: W/System.err(681):  at com.test.TestProjectActivity.onCreate(TestProjectActivity.java:56)
05-20 20:40:32.328: W/System.err(681):  at android.app.Activity.performCreate(Activity.java:4465)
05-20 20:40:32.338: W/System.err(681):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-20 20:40:32.348: W/System.err(681):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-20 20:40:32.358: W/System.err(681):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-20 20:40:32.368: W/System.err(681):  at android.app.ActivityThread.access0(ActivityThread.java:123)
05-20 20:40:32.378: W/System.err(681):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-20 20:40:32.388: W/System.err(681):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 20:40:32.398: W/System.err(681):  at android.os.Looper.loop(Looper.java:137)
05-20 20:40:32.408: W/System.err(681):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-20 20:40:32.418: W/System.err(681):  at java.lang.reflect.Method.invokeNative(Native Method)
05-20 20:40:32.428: W/System.err(681):  at java.lang.reflect.Method.invoke(Method.java:511)
05-20 20:40:32.438: W/System.err(681):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-20 20:40:32.448: W/System.err(681):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-20 20:40:32.448: W/System.err(681):  at dalvik.system.NativeStart.main(Native Method)
05-20 20:40:32.468: W/System.err(681): Caused by: java.net.ConnectException: socket failed: EACCES (Permission denied)
05-20 20:40:32.488: W/System.err(681):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:181)  
05-20 20:40:32.498: W/System.err(681):  ... 21 more
05-20 20:40:32.508: W/System.err(681): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
05-20 20:40:32.528: W/System.err(681):  at libcore.io.IoBridge.socket(IoBridge.java:573)
05-20 20:40:32.538: W/System.err(681):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
05-20 20:40:32.548: W/System.err(681):  at java.net.Socket.checkOpenAndCreate(Socket.java:663)
05-20 20:40:32.558: W/System.err(681):  at java.net.Socket.connect(Socket.java:807)
05-20 20:40:32.578: W/System.err(681):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-20 20:40:32.578: W/System.err(681):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-20 20:40:32.588: W/System.err(681):  ... 21 more
05-20 20:40:32.598: W/System.err(681): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
05-20 20:40:32.628: W/System.err(681):  at libcore.io.Posix.socket(Native Method)
05-20 20:40:32.658: W/System.err(681):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
05-20 20:40:32.658: W/System.err(681):  at libcore.io.IoBridge.socket(IoBridge.java:558)

Thanks.

谢谢。

回答by Dirk J?ckel

Did you declare the Internet permission in the AndroidManifest.xml?

您是否在 AndroidManifest.xml 中声明了 Internet 权限?

You need to put the following into the AndroidManifest.xml

您需要将以下内容放入 AndroidManifest.xml

<manifest> 
  ...
  <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

回答by Muaz Othman

internet permission to your application by adding this under manifesttag in your AndroidManifest.xml

通过manifest在您的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

otherwise Android will block internet traffic from your app

否则 Android 将阻止来自您的应用的互联网流量