0

I'm going to use HttpUrlConnection for calling a WebAPI. I simply call url.OpenConnectionMethod() to open the connection, but it doesn't work. Both allowUserInteraction and client.connected values of HtpUrlConnection instance are false.

URL url = new URL("http://myserver/service/api/ads/getads");
HttpURLConnection con = (HttpURLConnection) url.openConnection();

any help would be appreciated.

Vahid Ghadiri
  • 3,966
  • 7
  • 36
  • 45
  • did you add the permissions ? internet access. – Yazan Sep 08 '14 at 06:38
  • "Does not work" is a very broad term. Please specify what kind of trouble you're having. E.g. exceptions etc. – nfusion Sep 08 '14 at 06:40
  • @Yazan: Sure. Both Access_NetWork_State and Internet permissions. – Vahid Ghadiri Sep 08 '14 at 06:42
  • @nfusion: Actually I couldn't find any other terms for these kind of unusual bugs. As I mentioned after these two lines of code, the both connected and the allowUserInteraction values of HttpUrlConnection are false. but Ok I'm gonna to change the title of my question right now. thanks – Vahid Ghadiri Sep 08 '14 at 06:46

1 Answers1

1

Try this:

URL url = new URL("http://yourserver/x/y/z");
URLConnection urlConnection = url.openConnection();

if this doesn't work can I see any exceptions?

siegy22
  • 4,295
  • 3
  • 25
  • 43
  • ReVeN: this works well. but I want to know why HttpUrlConnection dose not work. If no one give answer, I'll definitely mark your answer as accepted one. – Vahid Ghadiri Sep 08 '14 at 06:48
  • @VahidGhadiri Try a normal webserver like google or something. Can you edit your post with the exceptions? – siegy22 Sep 08 '14 at 06:50
  • @ReVeN: There is no exception. after those two lines of code, i call con.getInputStream() and this is where the exception raise. because there is no available connection. – Vahid Ghadiri Sep 08 '14 at 06:54
  • I tested it with another WebService. It works fine. It seems it can't connect to just my service. do you have any suggestions? – Vahid Ghadiri Sep 08 '14 at 07:11
  • What IP do you use? something like localhost? try to use the IP from your host pc (ipconfig in cmd). – siegy22 Sep 08 '14 at 07:46