Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (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 
like image 347
abdfahim Avatar asked Nov 16 '13 07:11

abdfahim


3 Answers

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.

like image 173
Coderji Avatar answered Sep 30 '22 16:09

Coderji


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

like image 38
Junior Mayhé Avatar answered Sep 30 '22 15:09

Junior Mayhé


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

like image 43
Mr.Moustard Avatar answered Sep 30 '22 16:09

Mr.Moustard