0

Im trying to connect to a server and than receive some data back in Json format. Here is my java code:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://my.server.com/validate/" + data);
request.setHeader("Accept", "application/json");
request.setHeader("X-AuthToken","my authorisation code");
HttpResponse response = client.execute(request);

I also have

<permission android:name="android.permission.INTERNET"></permission>

as a child of manifest (not application) in my manifest.xml

When I try to open the url in browser, I get the correct page but I dont get any data since it misses the authentication. I also have a good connection.

I've been trying to fix this for a few hours now, but I still get IOException at the line with client.execute(request)

java.net.UnknownHostException: my.server.com

Any ideas? :) Thanks!

  • try hitting a known site like `http://www.google.com` see if you get the same result, if so you know that the problem is with connecting to internet for some reason. If not then you know problem is with your specific server – FoamyGuy Nov 29 '12 at 15:16
  • Are you running the app on Android 3.0 or higher? – Bigflow Nov 29 '12 at 15:17
  • I tryed google, the same result. Im running on Android 2.3.3 – Tomáš 'Guns Blazing' Frček Nov 29 '12 at 15:20
  • Oh ok, that shouldn't give a problem then. in 3.0 and higher you must put it in a Async task, else it won't work. But like I said, that is only in 3.0 and higher. – Bigflow Nov 29 '12 at 15:22

3 Answers3

5

Seems like you're missing uses-?

<uses-permission android:name="android.permission.INTERNET" />?

<permission> is used to declare a new permission. Docs

dmon
  • 30,048
  • 8
  • 87
  • 96
  • wow, that was pretty stupid of me...I usually write manifest file manualy, this time I used the Eclipse GUI for it...anyway, that fixed the problem, thanks! – Tomáš 'Guns Blazing' Frček Nov 29 '12 at 15:29
  • @Tomáš'GunsBlazing'Frček Inside of eclipse manifest GUI, on Permissions tab. After you click `Add...` you need to choose `uses-permission` which is the bottom on on the list – FoamyGuy Nov 29 '12 at 15:30
  • Yeah, it is unnecessarily confusing :/ – dmon Nov 29 '12 at 16:01
0

Sorry if I'm missing something, but is my.server.com really a valid host? Did you forget to plug in the actual host?

chinabuffet
  • 5,278
  • 9
  • 40
  • 64
0

Besides the answers here, here are a few aditional suggestions

  1. Try going to to URL in a browser
  2. Check to make sure you have an internet connection on your device by using the browser
  3. Try cleaning the project and trying again (this has done the trick for me several times) (Project -> Clean)
Jameo
  • 4,507
  • 8
  • 40
  • 66