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());
}
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.
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.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)
Thanks.
Did you declare the Internet permission in the AndroidManifest.xml?
You need to put the following into the AndroidManifest.xml
<manifest>
...
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
internet permission to your application by adding this under manifest
tag in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
otherwise Android will block internet traffic from your app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With