本文介绍了为什么我收到HttpHostConnectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的code在Android的虚拟机
I am using the following code in android virtual Machine
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。不知道为什么?我从127.0.0.1更改地址的网址为10.0.2.2,但仍然得到了异常。我已经瓦帕服务器安装在我的电脑并放置在www的文件夹,该文件ReadingFromServer.php
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.
下面是完整的堆栈跟踪
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权限?
Did you declare the Internet permission in the AndroidManifest.xml?
您需要把以下到AndroidManifest.xml中
You need to put the following into the AndroidManifest.xml
<manifest>
...
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
这篇关于为什么我收到HttpHostConnectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!