24

I am trying to access from an Android device over WiFi a local web server which I can access from my laptop either on the browser, or using curl. I can also access the server on the android device browser.

The code I am using to access the server yields a "connection refused" exception.

This is the code:

public void getController1() {
  HttpClient httpclient = new DefaultHttpClient();
  HttpGet httpget = new HttpGet("http://192.168.1.169:8000");
  HttpResponse response = null;
  System.out.println(httpget.toString());
  try {
    response= httpclient.execute(httpget);
    txtViewStatus.setText("Controller 1 - OK"+response);
  } catch(Exception e) {
    e.printStackTrace();
    txtViewStatus.setText("Controller 1 - Error"+e);
  }
}
Smita Ahinave
  • 1,901
  • 7
  • 23
  • 42
Tori
  • 1,358
  • 4
  • 19
  • 38
  • 4
    do you have the internet permission in your manifest ? – njzk2 Feb 27 '12 at 17:09
  • to be clear, you can access `192.168.1.169:8000` from the browser of the device that gives you `connection refused`? – KevinDTimm Feb 27 '12 at 17:09
  • Yes, I do have INTERNET permission in the manifest. – Tori Feb 27 '12 at 17:11
  • Yes, I can access the server from the device browser that give the connection refused. – Tori Feb 27 '12 at 17:11
  • 1
    It's your webserver that does not like your request. Might be caused by not specifying a user-agent. You could try it with `HttpClient httpClient = AndroidHttpClient.newInstance("Android")` which will set that to "Android" and also have some nicer defaults than the `DefaultHttpClient` – zapl Feb 27 '12 at 17:23
  • 1
    Same behavior using the AndroidHttpClient instead of the DefaultHttpClient – Tori Feb 27 '12 at 18:00
  • Problem solved. The issue was a corrupted manifest file. I deleted the permission lines and re-typed them in and now the problem is gone. – Tori Feb 27 '12 at 18:48

6 Answers6

24

Problem solved. The issue was a corrupted manifest file. I deleted the permission lines and re-typed them in and now the problem is gone

Tori
  • 1,358
  • 4
  • 19
  • 38
9

I was missing permission; you should check manifest file

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    
 <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
DPP
  • 12,716
  • 3
  • 49
  • 46
4

Check your internet connection strength.

For my case the network connection is low so this error thrown.

Sampath Kumar
  • 4,433
  • 2
  • 27
  • 42
3

Past this to your manifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
r-magalhaes
  • 427
  • 2
  • 9
  • 18
1

Can it be because you're using a proxy? The proxy issue is discussed in Android HttpClient Doesn't Use System Proxy Settings (see the answer by CommonsWare).

Community
  • 1
  • 1
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

you should enable pc suite in android device :

1: set http url in android app e.g ("http://192.168.1.100:81/api/droid/getdata")
laptop or pc wifi ip is (192.168.1.100)
2: connect android device to pc or laptop in usb port
3: goto this setting in android device setting -> more -> Tethering and portable hotspot -> pc suite(HiSuite)
4: run android app

this work good

HamidReza
  • 1,726
  • 20
  • 15