6

I have an apache server installed in my PC where I host one PHP file.

Then I tried to connect to that file using eclipse, but it always gives me below error

connection to http://10.0.2.2:8080 refused.

I tried changing address to the followings, but got similar error all the time.

http://10.0.2.2
http://127.0.0.1
http://localhost

Can anyone please help me.

EDITED: Just for information, I can access to remoter server (e.g. www.mydomain.com) without any problem.

CLASS FILE:

HttpClient httpclient = new DefaultHttpClient();
Log.d("test","t0");
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/mylibman/data.php");
Log.d("test","t1");
HttpResponse response = httpclient.execute(httppost); // error here
Log.d("test","t2");

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.migrationdesk.mylibman"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET" />

Error LogCat:

11-16 02:02:20.803: E/log_tag(1427): Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
11-16 02:02:20.803: E/log_tag(1427): Error converting result java.lang.NullPointerException: lock == null
11-16 02:02:20.803: E/log_tag(1427): Error parsing data org.json.JSONException: End of input at character 0 of 
abdfahim
  • 2,523
  • 10
  • 35
  • 71

5 Answers5

2

alright go to Apache httpd.conf (located in Apache folder): and search for

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

and check if the second line is Deny, if it is then change it to:

Allow from all

then restart the Apache server, and tell me the feedback.

Edit

try it out in your real device:

go to CMD and type ipconfig under IPv4 take the IP address and change the IP it will look similar to this:

http://192.168.0.106:8080/mylibman/data.php // similar to this.

Turn Off the firewall and any anti-virus application in your PC

and please give me the feedback.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Coderji
  • 7,655
  • 5
  • 37
  • 51
  • 1
    sorry for being too late .. I had to be out for some time ... anyhow, the point is, even in real device it works, I need it in emulator. As I told, if I put internet link it works fine. But I need to check my coding offline .... – abdfahim Nov 17 '13 at 13:41
  • what server are you using? IIS or XAMPP??1 – Coderji Nov 17 '13 at 14:02
  • Apache through EasyPHP – abdfahim Nov 17 '13 at 14:46
  • @Coderji All of solutions doesn't work. I'm using wampserver, and test on both emulator and real device. – Dr.jacky Jul 02 '14 at 11:08
  • For new Apache version, use `Require all granted` instead of `Allow from all` . For details, read [Access Control](http://httpd.apache.org/docs/current/upgrading.html#access) – Sen Jacob May 02 '16 at 18:28
1

Mike helped me a lot on this issue. So here my steps to avoid this Connection refused.

1) Add if to test Android SDK. It seems that in new versions you have to explicity allow the connection to be open in a different thread:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            System.out.println("*** My thread is now configured to allow connection");
        }

...

2) Add an exception for your port:

in your case allow inbound connection for port 8080

3) test your connection by browsing the desired url locally

use your browser and try to visit http://10.0.2.2:8080 locally

4) Add in your manifest file a permission for internet:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourapp"
    android:versionCode="1"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

I am using Wamp, and its apache is listening to servername localhost and port 8080 (allow from all in httpd.conf).

My local IP is something similar to 192.168.0.XXX/folder/my-page-here

Community
  • 1
  • 1
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
1

You can use your real IP address to reach your virtual server. Take a look at my answer here. I hope it helps.

Community
  • 1
  • 1
Mr.Moustard
  • 1,297
  • 21
  • 24
0

if your SDK version is less then 9 then add above code for SDK version them some changes in Apache update httpd.conf as line deny from all..replace that sentence Allow from all and restart services of WAMP server then use 10.0.2.2 address or 10.0.2.2:8080 port no and include permission line in android AndroidManifest file

<uses-permission 
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
zheek
  • 742
  • 1
  • 11
  • 22
0

I fix this problem by writing my IP address from cmd -> ipconfig in the url :

 HttpPost httppost = new HttpPost( "http://192.168.x.x/mylibman/data.php" );
zheek
  • 742
  • 1
  • 11
  • 22
seby598
  • 141
  • 4
  • 11