Skip to content

constructor ReadableNativeMap in class ReadableNativeMap cannot be applied to given types; #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
BigPun86 opened this issue Jun 29, 2016 · 8 comments

Comments

@BigPun86
Copy link

BigPun86 commented Jun 29, 2016

When integrating this module to my android project i get the error code as in the title mentioned.

..../React/ChatTest/node_modules/react-native- socketio/android/src/main/java/com/gcrabtree/rctsocketio/SocketIoReadableNativeMap.java:16: error: constructor ReadableNativeMap in class ReadableNativeMap cannot be applied to given types;
public class SocketIoReadableNativeMap extends ReadableNativeMap {
   ^
  required: HybridData
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
@mcliquid
Copy link

+1

@littlepsylo
Copy link

Same here and with react-native 0.30.0

@scizers
Copy link

scizers commented Jul 28, 2016

Same here

@daywong1119
Copy link

same here

@ankuj
Copy link

ankuj commented Aug 1, 2016

same here,
@gcrabtree any solution that you could suggest?
thanks

@littlepsylo
Copy link

Sorry for the author but i could'nt wait more... so if this can help you guys: https://gist.github.com/littlepsylo/341ed91aed9870924de082d9d1eee580

This simple implementation of WebSocket works for me with an existing socket.io server on both android and ios. Beyond that, you will surely find more informations in the React-Native documentation or MDN, for example, about this API.

@maltheism
Copy link

maltheism commented Aug 3, 2016

#8
Solution is to add this to SocketIoReadableNativeMap.java:

protected SocketIoReadableNativeMap(HybridData hybridData) {
    super(hybridData);
}

SocketIoReadableNativeMap.java then looks like this:

package com.gcrabtree.rctsocketio;

import android.util.Log;

import com.facebook.jni.HybridData;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableNativeMap;

import java.util.HashMap;

import io.socket.client.IO;

/**
 * Created by Greg Crabtree on 5/17/16.
 */

public class SocketIoReadableNativeMap extends ReadableNativeMap {
    private static final String TAG = "SIOReadableNativeMap";

    protected SocketIoReadableNativeMap(HybridData hybridData) {
        super(hybridData);
    }

    /**
     * Note: This will only be necessary until RN version 0.26 goes live
     * It will be deprecated from the project, as this is just included in that version of RN.
     *
     * This converts the SocketIoReadableNativeMap to a Java usable HashMap.
     * @return converted HashMap.
     */
    public static HashMap<String, Object> toHashMap(ReadableNativeMap map) {
        ReadableMapKeySetIterator iterator = map.keySetIterator();
        HashMap<String, Object> hashMap = new HashMap<>();

        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            switch (map.getType(key)) {
                case Null:
                    hashMap.put(key, null);
                    break;
                case Boolean:
                    hashMap.put(key, map.getBoolean(key));
                    break;
                case Number:
                    hashMap.put(key, map.getDouble(key));
                    break;
                case String:
                    hashMap.put(key, map.getString(key));
                    break;
                case Map:
                    hashMap.put(key, toHashMap(map.getMap(key)));
                    break;
                case Array:
                    hashMap.put(key, ((SocketIoReadableNativeArray) map.getArray(key)).toArrayList());
                    break;
                default:
                    throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
            }
        }
        return hashMap;
    }

    /**
     * This converts a SocketIoReadableNativeMap to a SocketIO Option object.
     * @param options ReadableNativeMap that is a JS bridged hash of options.
     * @return IO.Options object that has been populated. Currently incomplete. Pas welcome.
     */
    public static IO.Options mapToOptions(ReadableNativeMap options) {
        ReadableMapKeySetIterator iterator = options.keySetIterator();
        IO.Options opts = new IO.Options();

        while (iterator.hasNextKey()) {
            String key = iterator.nextKey().toLowerCase();
            switch (key) {
                case "force new connection":
                case "force new"
                    opts.forceNew = options.getBoolean(key);
                    break;
                case "multiplex":
                    opts.multiplex = options.getBoolean(key);
                    break;
                case "reconnection":
                    opts.reconnection = options.getBoolean(key);
                    break;
                case "connect_timeout":
                    opts.timeout = options.getInt(key);
                    break;
                default:
                    Log.e(TAG, "Could not convert object with key: " + key + ".");
            }
        }
        return opts;
    }
}

Note:
You can't use localhost when creating a new SocketIO for Android Emulator.
Use whatever your local IP is when creating a socket in index.android.js
Example:
this.socket = new SocketIO('http://192.168.0.1:3000', {});

@saalihou
Copy link

Made a PR that makes this library compatible with RN 0.29+
Check it out here: #46

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

Successfully merging a pull request may close this issue.

8 participants