简体   繁体   中英

Connecting localhost via android - Connection to 10.0.2.2 refused

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 (eg 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 

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 Appache server, and tell me the feadback.

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.

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

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

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>

我通过在url中写入cmd -> ipconfig IP地址来解决这个问题:

 HttpPost httppost = new HttpPost( "http://192.168.x.x/mylibman/data.php" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM