难道Android Wear支持的HttpURLConnection - 让EOFException类 [英] Does Android Wear support HttpURLConnection - getting EOFException

查看:830
本文介绍了难道Android Wear支持的HttpURLConnection - 让EOFException类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我们可以通过访问网络的HttpURLConnection Android Wear

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

我想里面穿code。使用HttpURLConnection类,我得到的 EOFException类。同样的code ++工程,从普通的Andr​​oid手机。它只有问题,当它在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.

如果HttpURLConnection的不支持磨损,我们应该使用Apache HTTP客户端还是其他什么东西?

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(); 

我没有添加权限到Android清单。我也从AsyncTask的运行上面的code。

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

EOFException类发生在 conn.getResponse code()

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.

推荐答案

不幸的是,没有。

Android Wear应用不能直接访问因特网。他们必须与其相应的手持设备的应用程序进行通信(通过<一href="https://developer.android.com/reference/com/google/android/gms/wearable/MessageApi.html">MessageApi或<一href="https://developer.android.com/reference/com/google/android/gms/wearable/DataApi.html">DataApi)并要求它执行任何HTTP请求,你需要的。

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.

这篇关于难道Android Wear支持的HttpURLConnection - 让EOFException类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆