gpt4 book ai didi

java - 为什么我收到 HttpHostConnectException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:52 25 4
gpt4 key购买 nike

我在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());
}

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

这是完整的堆栈跟踪

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.access$600(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)

谢谢。

最佳答案

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

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

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

关于java - 为什么我收到 HttpHostConnectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10676861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com