相关文章推荐
酷酷的煎鸡蛋  ·  Golang WebSocket Ping ...·  2 周前    · 
飘逸的荔枝  ·  WebSockets support in ...·  6 天前    · 
咆哮的木瓜  ·  超赞!EMNLP2023 | ...·  10 月前    · 
风度翩翩的凳子  ·  java http chunked ...·  1 年前    · 
阳刚的小马驹  ·  PostgreSQL IDE ...·  1 年前    · 
儒雅的梨子  ·  ASP.NET HtmlTable 控件 ...·  1 年前    · 
玩命的吐司  ·  python sns.jointplot ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

today my problem is that I am developing an app that is using a WebSocket. On iOS everything is working out fine but on android the WebView doesn't support the WebSocket. Whats the fastest/most elegant way to solve this problem?

[edit]

As this post is viewed quite often I want to share some possible solutions.

Solutions:

  • Java Websocket Client Implementation & JavaScript Interface
  • PHP Websocket Client Implementation & making a background (i.e. jQuery GET) request to the PHP-script (worked for me as I wanted to send only one line of message.)
  • Android Websocket Libraries

  • http://autobahn.ws/
  • http://code.google.com/p/weberknecht
  • Someone had the same issue and descriped how he worked around

  • http://chpeck.tumblr.com/post/37273036645/socket-io-with-android-webview
  • I created a sample project which adds the WebSocket object to the WebView for phonegap. You can find it here: https://github.com/pusher/pusher-phonegap-android

    I put up a video going over the project here: http://blog.pusher.com/2012/7/5/pusher-on-phonegap-for-android

    I used this project to demonstrate how to use PhoneGap with Pusher on Android but ultimately it's just a project that happens to include the Pusher script tag. You could easily use another library that needs the WebSocket object or just use it directly.

    It uses the websocket-android-phonegap library: https://github.com/anismiles/websocket-android-phonegap

    Do you include your PhoneGap App in an Android WebView or just opened in the Android Browser? Langusten Gustel Nov 26, 2012 at 13:15 The sample project creates a PhoneGap app which uses Android WebView with an injected WebSocket object. This means you can then use WebSocket within the JavaScript code running in the Android WebView. leggetter Nov 26, 2012 at 21:02 Yes. You write your code in JavaScript and it uses the injected WebSocket object which proxies the calls between the JavaScript and Java wrappers. You can use the project and code nothing other than HTML, JavaScript & CSS. Try it out and you'll see what I mean. leggetter Nov 27, 2012 at 18:34 Can I also use HTML5 geolocation in this WebView? For instance, I want to track users of the app in real time using geolocation. Ben Sewards Mar 5, 2013 at 0:21 As far as I'm aware the WebView is an instance of the stock Internet browser app. So, if it supports geolocation then yes, you could do that. leggetter Mar 5, 2013 at 11:26

    Android webview doesn't accommodate WebSocket. So you need to implement with some WebSocket library.

    jWebSocket http://jwebsocket.org/

    weberknecht http://code.google.com/p/weberknecht/

    What if I want to go it without chaning the HTML/JS Source. What do you think of overriding the JS-WebSocket Object by an JavaScript Interface? Langusten Gustel Nov 22, 2012 at 8:54

    check your error code first;if the code is :ERR_CLEARTEXT_NOT_PERMITTED; Try to add android:usesCleartextTraffic="true" to you application tag in your AndroidManifest.xml like below:

     <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.