Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

ClassCastException on onAllMarketTickersEvent stream #72

Closed
tompee26 opened this issue Feb 16, 2018 · 11 comments
Closed

ClassCastException on onAllMarketTickersEvent stream #72

tompee26 opened this issue Feb 16, 2018 · 11 comments
Labels

Comments

@tompee26
Copy link

I am getting a class cast exception when using the market ticker event stream.

Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.binance.api.client.domain.event.AllMarketTickersEvent

Is anyone else having this issue? Code below for reference.

mWebSocketClient.onAllMarketTickersEvent(new BinanceApiCallback<List<AllMarketTickersEvent>>() {
    @Override
    public void onResponse(List<AllMarketTickersEvent> events) throws BinanceApiException {
        Log.d(TAG, events.get(0).toString());
    }
});
@tompee26
Copy link
Author

I think the reason here is that Q A v q cannot be parsed as long. Maybe because of a recent update.

@MetalMiner
Copy link

Yes, I am facing the same issue and it is really annoying. Pleace fix this fast!

@MetalMiner
Copy link

MetalMiner commented Feb 19, 2018

The problem is the constructor

public BinanceApiWebSocketListener(BinanceApiCallback<T> callback) { this.callback = callback; this.eventTypeReference = new TypeReference<T>() {}; }

You can't get the Typereference of the callback that way. As a quick fix change the onMessage method of the same class as follows:

@Override public void onMessage(WebSocket webSocket, String text) { if (webSocket.request().url().toString().endsWith("ticker@arr")) { eventTypeReference = (TypeReference<T>) new TypeReference<List<AllMarketTickersEvent>>(){}; } ObjectMapper mapper = new ObjectMapper(); try { T event = null; if (eventClass == null) { event = mapper.readValue(text, eventTypeReference); } else { event = mapper.readValue(text, eventClass); } callback.onResponse(event); } catch (IOException e) { throw new BinanceApiException(e); } }

This is only a temporary solution of course.

Furthermore you have to change the type of Q A v q in AllMarketTickersEvent to Double as mentioned before.

@joaopsilva
Copy link
Member

Hi everyone, I'm unable to reproduce this problem, please make sure you update your repository and you're using the latest version, since there were some recent updates on the websocket-based client.

@joaopsilva
Copy link
Member

For reference, I'm using Java(TM) SE Runtime Environment (build 1.8.0_60-b27)

@MetalMiner
Copy link

I think this is an issue related to the Runtime Environment. I switched to yours an it is working fine. When I am using my default which is 1.8.0_144 it is throwing errors. Could you pleace consider testing the API with newer Runtime versions?

@0xParts
Copy link

0xParts commented Feb 25, 2018

Have the same problem. Here is another example for a quick fix:

            List<AllMarketTickersEvent> tick = new ArrayList<AllMarketTickersEvent>();
            client.onAllMarketTickersEvent(response -> {
                    String str = response.toString();
                    int i=0, c;
                    for(int x = 0; x < response.size(); x++) {
                            tick.add(new AllMarketTickersEvent());
                            i = str.indexOf('s', i)+2;
                            c = str.indexOf(',', i);
                            tick.get(x).setSymbol(str.substring(i, c));
                            i = str.indexOf('c', i)+2;
                            c = str.indexOf(',', i);
                            tick.get(x).setCurrentDaysClosePrice(Double.parseDouble(str.substring(i, c)));                                                                             
                    }       
            });

@gokmenkisakol
Copy link

gokmenkisakol commented Apr 6, 2018

Hi, I used this code;

for (Iterator iter = response.iterator(); iter.hasNext(); )
{
ObjectMapper mapper = new ObjectMapper();
AllMarketTickersEvent allMarketTickersEvent = mapper.convertValue(iter.next(), AllMarketTickersEvent.class);
}

@mjwchapman
Copy link

Pull request #117 fixes this issue.

@wrone
Copy link

wrone commented Sep 6, 2018

Still not fixed? Struggling with it 👎

@joaopsilva
Copy link
Member

This is now fixed, please update your repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants