I have a working .Net WCF HTTP service. If I put the URL into I.E, FireFox, or google Chrome browsers, I get back the correct stream. But when I try to access that same service in an android app (running in Eclipse) I get a socketException with the detail message “Permission Denied”. The service is running on my machine in the Visual Studio web hoster and the client is running in the android emulator also on my machine.
The line that gets the exception is:
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
(I have an earlier post on this same program where the url is http://www.android.com and I’m getting “invalid url”)
Might anyone have a clue why the browsers have no problem with it and the android client is getting permission denied?
thanks,
Gary
HttpURLConnection urlConnection = null;
try
{
URL url = new URL("http://localhost:8080/TeamTask/Tasks");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
A permission denied is most likely caused by not having the permission in your AndroidManifest.xml file.
Also, and I haven’t tested it, but I would imagine that using “localhost” would not work to begin with because it is running on an emulator, just as a VM machine using localhost points back to the VM, not the host machine — put in your machine’s IP address. I’m currently testing some Android HTTP calls from a different machine, on the same network, and I simply input my server’s local IP.
EDIT:
Finally, here is some tried and true code I use on a regular basis for making simple HTTP call: