1

I know the procedure in eclipse but in android studio it's difficult

This is the link of library

https://github.com/koush/android-websockets

Rajat Arora
  • 31
  • 1
  • 3

2 Answers2

3

The README.md in the library says DEPRECATED big and fat and provides a link to a better solution: https://github.com/koush/AndroidAsync. If you use this library simply add this line to you gradle dependencies:

compile 'com.koushikdutta.async:androidasync:2.+'
finki
  • 2,063
  • 1
  • 20
  • 23
  • but how to add this in my project. i mean in which folder? – Rajat Arora Jul 28 '15 at 14:47
  • you don't need to add this to a folder. that's not how the gradle build system works. you specify your dependencies in your build.gradle file and android studio (or rather gradle) downloads and stores them for you. you don't have to worry about anything. – finki Jul 28 '15 at 14:48
  • do i have to include anything in settings.gradle? – Rajat Arora Jul 28 '15 at 14:57
  • no, only in build.gradle, the file where you defined your compileSdkVersion, minSdkVersion, etc. There should be a block called dependencies { ... } with some dependencies in it. If not, create one and add the dependency from above – finki Jul 28 '15 at 15:01
  • dude it's showing the error " could not find compile method for arguments[compile 'com.koushikdutta.async:androidasync:2.+']" – Rajat Arora Jul 28 '15 at 15:12
  • please post your build.gradle file so we can help you. – finki Jul 28 '15 at 15:16
  • that's the way I meant it. It works for me, so your error has to be somewhere else. Did you try a gradle clean and rebuild? – finki Jul 28 '15 at 15:31
  • this is the code of MainActivity.java .... 14 errors are showing in this http://hastebin.com/delohacexo.java – Rajat Arora Jul 28 '15 at 15:45
  • what should i do with this class? can't compile this because of missing classes^^ if you want help give me please a git repo url to clone. – finki Jul 29 '15 at 09:36
  • Can u give the link of yours facebook or google+ profile? – Rajat Arora Jul 31 '15 at 21:31
2

Use this library org.java_websocket

First thing you should import that library in build.gradle

repositories {
     mavenCentral()
}

then add the implementation in dependency{}

implementation "org.java-websocket:Java-WebSocket:1.3.0"

Then you can use this code

 private void ConnectToWebSocket() {
    URI uri;
    try {
        uri = new URI("ws://192.168.1.135:9000/");
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return;
    }

    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
        }

        @Override
        public void onMessage(String s) {
            final String message = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView textView = (TextView)findViewById(R.id.edittext_chatbox);
                    textView.setText(textView.getText() + "\n" + message);
                }
            });
        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.i("Websocket", "Closed " + s);
        }

        @Override
        public void onError(Exception e) {
            Log.i("Websocket", "Error " + e.getMessage());
        }
    };
    mWebSocketClient.connect();
}
Muhammed Fasil
  • 7,909
  • 2
  • 19
  • 28