我在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)
感谢。
答案 0 :(得分:14)
您是否在AndroidManifest.xml中声明了Internet权限?
您需要将以下内容放入AndroidManifest.xml
<manifest>
...
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
答案 1 :(得分:1)
manifest
中的AndroidManifest.xml
标记下添加此权限,对您的应用程序的互联网权限
<uses-permission android:name="android.permission.INTERNET" />
否则Android将阻止您应用的互联网流量