0

I'm trying to get some data from a trading platform (https://www.binary.com). The code samples for getting data from them is provided at the developers' site (https://developers.binary.com/demos/).

Since I was using android studio, I opted for java, copied the code, created a new class, and pasted the code there.

Here's my code:

package com.example.smijes.myapplication;  
import java.net.URI;  
import java.io.IOException;  
import java.lang.InterruptedException;  
import javax.websocket.*;

@ClientEndpoint  
public class WSClient  {

    @OnOpen
    public void onOpen(Session session) throws java.io.IOException {
        session.getBasicRemote().sendText("{\"ticks\": \"R_100\"}");
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("ticks update: " + message);
    }

    public static void main(String[] args)
            throws IOException, DeploymentException, InterruptedException {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        URI apiUri = URI.create("wss://ws.binaryws.com/websockets/v3?app_id=1089");
        Session session = container.connectToServer(WSClient.class, apiUri);
        Thread.sleep(10000);
    }
}

I noticed some errors, did some research then added a dependency to my app level build.gradle file:

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

After synchronizing gradle, I noticed the error persisted.

Here is a screenshot to show the errors:

javax-websocket-error

I think the entire problem is from the import javax.websocket.*; line of code. I also think the dependency I added doesn't correspond. Can the dependency I added be for java.websocket rather than javax.websocket?

I'm using Android Studio 3.1.3.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
SMIJ
  • 1
  • 4
  • 1
    Please **[format](https://stackoverflow.com/help/formatting)** your code and image properly into the post, instead of using external sites. And have you checked this: [javax.* cannot be imported in my Android app?](https://stackoverflow.com/q/16803343/2745495)? – Gino Mempin Jul 05 '18 at 23:49
  • I've formatted the code. I tried adding the image but failed. – SMIJ Jul 06 '18 at 00:39
  • I've also checked out the link u provided, but their problem/solution there didn't appear relevant to my course. – SMIJ Jul 06 '18 at 01:05
  • Sorry, I should have explicitly pointed you to the [answer](https://stackoverflow.com/a/16811778/2745495) where it says that not all `javax.*` libraries are available when compiling code for Android, which _Android Studio is built for_. In your case, Android Studio cannot find `javax.websocket`. Is there a reason you need to use Android Studio and not some other Java IDE? – Gino Mempin Jul 06 '18 at 01:10
  • I actually checked the list of APIs supported by dalvik VM as he provided there but didn't see javax.websocket but saw java.net.ssl(looks closest to what i need).started with android studio and that's the only IDE am familiar with. – SMIJ Jul 06 '18 at 01:49
  • I'll prefer sticking with android studio though, that is, if there's anyway i can achieve my aim with it. If not , i don't mind switching to an IDE that will work just fine. – SMIJ Jul 06 '18 at 01:52
  • I don't know the exact solution for building `javax.*` within Android Studio. What I can suggest is that you try your code using a generic/all-purpose Java IDE instead (since your target app isn't really Android-specific). – Gino Mempin Jul 06 '18 at 06:00
  • by "generic/all-purpose", pls what do u mean ? – SMIJ Jul 06 '18 at 14:37

2 Answers2

0

To solve the problem, all you need to to do is to download the required jar files(javax.json-1.0.jar and tyrus-standalone-client-1.9.jar in my case), add (extract it from from the zipped folder, then copy and paste) to your assets resource folder on android studio. After this, locate your packages and add those jar files to your libraries. With the appropriate import statements, the errors should go away.

SMIJ
  • 1
  • 4
0

What @SMIJ said helped me too! Simply insert and use the following dependencies in the build.gradle:

implementation group: 'javax.json', name: 'javax.json-api', version: '1.0'
implementation group: 'org.glassfish.tyrus.bundles', name: 'tyrus-standalone-client', version: '1.9'