Android: Has anyone encountered the HttpClient Connection localhost error?
I'm just wondering if anyone has resolved this issue. Googling gives tons of posts having this question, but not one with the correct answer. I've tried literally every combination of the following two pieces of code, with and without a 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:
- Setting up a proxy in Eclipse preferences
- Setting up my own localhost and writing a little php script that takes the url, connects to it, and then gets the file from the intranet site - this works from the browser! It doesn't work when I use the IP address 10.0.2.2.
Any thoughts?
+2
source to share
4 answers
You should check this out: 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")
+13
source to share
You need to grant the appropriate permissions to the app in order for it to use the internet.
try adding the following line to your app 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 it worked for me.
+3
source to share