9

I am wondering if we can access the network through HttpURLConnection from Android Wear?

I tried using HttpURLConnection inside Wear code, I am getting EOFException. The same code works from regular Android phone. It only has problem when it is on Android Wear.

If HttpURLConnection is not supported on Wear, should we use Apache Http client or something else?

Or perhaps the way I am launching the app for development is incorrect?

        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect(); 
        int response = conn.getResponseCode(); 

I did add the permission into android manifest. I also run the above code from an AsyncTask.

EOFException occurs at conn.getResponseCode().

java.io.EOFException
        at com.android.okhttp.internal.Util.readAsciiLine(Util.java:342)
        at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:311)
        at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:353)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:297)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:509)

Thank you very much for your help.

ssw
  • 175
  • 1
  • 9

1 Answers1

10

Unfortunately, no.

Android Wear applications cannot directly access the Internet. They must communicate with their corresponding handheld app (either via MessageApi or DataApi) and request that it executes whatever HTTP requests you need.


EDIT: Android Wear 2.0, now in beta, supports network requests, so HttpURLConnection should work there.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Does that mean that for the wear device to access the internet, the wear device has to talk to the mobile phone app. then the mobile phone app has to act as a proxy? – ssw Jul 13 '14 at 12:37
  • @ssw Yes, precisely. At least as things stand now. – matiash Jul 13 '14 at 15:41
  • 1
    Thanks for the reply. I still cannot find any mention that the networking stuff is not supported. The funny thing is that the android wear developer page does not say that networking stuff is not supported. It only mentions that the following packages are not supported: android.webkit, android.print, android.app.backup, android.appwidget, android.hardware.usb. source: http://developer.android.com/training/wearables/apps/index.html – ssw Jul 14 '14 at 23:08
  • @ssw I understand your point about the lack of official documentation on the subject. But the fact remain that direct internet access from the wearable is not possible right now. Whether a design decision (probably to save battery life) or a temporal limitation, it's unknown, but it doesn't work right now. – matiash Jul 15 '14 at 00:20
  • i was really hoping to confirm from official source that it is really not supported. but the documentation really did not mention it one way or the other. thanks for your reply. – ssw Jul 16 '14 at 01:33
  • http://developer.android.com/training/wearables/data-layer/index.html has a warning section that says: "Warning: Because these APIs are designed for communication between handhelds and wearables, these are the only APIs you should use to set up communication between these devices. For instance, don't try to open low-level sockets to create a communication channel." Sorry :( – Abhay Buch Jul 16 '14 at 17:30
  • 1
    @AbhayBuch yep. but i interpreted that to mean that we should use Wearable Data Layer api for communication between **handhelds and wearables**. And we should not use our own socket to communicate between **handhelds and wearables**. but i thought that means communication using socket to outside world is possible, we just do not want to use it between **handhelds and wearables**. – ssw Jul 18 '14 at 00:26