Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Has anyone encountered the localhost HttpClient Connection Refused error?

Tags:

android

http

I was just wondering if anyone solved this problem. Googling gives tons of posts having this question but not one with a proper reply. I tried literally every combination of the following two pieces of code with and without proxy:

/*********** URL METHOD ***************/
//URLConnection conn = aURL.openConnection(); 
//conn.connect(); 
//InputStream is = conn.getInputStream(); 

/*********** HTTP METHOD ***************/
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(urlString);
HttpResponse resp = client.execute(get);

InputStream is = resp.getEntity().getContent();

I am trying to connect to a site on my intranet (its not localhost). I've tried the following:

  1. Setting Proxy inside Eclipse settings
  2. Setting my own localhost and writing a small php script that takes the url, connects to it and then gets the file from the intranet site - This works from the Browser though! It does not work when I use the 10.0.2.2 IP address

Any thoughts?

like image 704
Legend Avatar asked Nov 25 '25 22:11

Legend


2 Answers

You should check out this : http://developer.android.com/guide/appendix/faq/commontasks.html#localhostalias

(use the alias "10.0.2.2" instead of "localhost" or "127.0.0.1")

like image 151
lethargicpanda Avatar answered Nov 28 '25 16:11

lethargicpanda


You need to give proper permissions to the app in order for it to use internet.

try adding following line in your application manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage"
    android:versionCode="1"
    android:versionName="1.0" >
    ..
    ..
    <uses-permission android:name="android.permission.INTERNET" />
</manisfest>

At least this worked for me.

like image 24
matangs Avatar answered Nov 28 '25 17:11

matangs