How to implement a native interface that has 2 methods with the same name and number of arguments


#1

I’m currently in the process of implementing the support for self-signed certificates for HTTPS requests, with the typical technique consisting in overriding the default SSLSocketFactory. Of course this does not work with the standard client because the Android implementation of okhttp overrides the override (yay!).
So I’m trying to use okhttp3 directly.
It works so far for http requests, but for websockets I need to provide a WebSocketListener and this fails with JS: ERROR Error: Cannot marshal JavaScript argument [object Object] at index 0 to Java type..

I already managed to pass interfaces to java to set the trust certificates for instance. But for this interface, it fails. Something that might be a problem is that this interface has 2 methods with the same name and the same number of parameters.

okhttp3.d.ts

...
interface IWebSocketListener {
	onClosed(webSocket: WebSocket, code: number, reason: string): void
	onClosing(webSocket: WebSocket, code: number, reason: string): void
	onFailure(webSocket: WebSocket, t: java.lang.Throwable, response: Response): void
	onOpen(webSocket: WebSocket, response: Response): void
	onMessage(webSocket: WebSocket, text: string): void
	onMessage(webSocket: WebSocket, text: okio.ByteString): void
}

class WebSocketListener implements IWebSocketListener {
	constructor(implementation: IWebSocketListener)

	onClosed(webSocket: WebSocket, code: number, reason: string): void
	onClosing(webSocket: WebSocket, code: number, reason: string): void
	onFailure(webSocket: WebSocket, t: java.lang.Throwable, response: Response): void
	onOpen(webSocket: WebSocket, response: Response): void
	onMessage(webSocket: WebSocket, text: string): void
	onMessage(webSocket: WebSocket, text: okio.ByteString): void
}
...

call site:

const l = new okhttp3.WebSocketListener({
	onOpen: (webSocket: okhttp3.WebSocket, response: okhttp3.Response): void => {
		console.log("WebSocket opened: " + response.request().url());
	},
	

	onMessage: (webSocket: okhttp3.WebSocket, text: string): void => { // matches 2 methods in java
		console.log("WebSocket received: " + text);
	},

	onClosing: (webSocket: okhttp3.WebSocket, code: number, reason: string): void => {},
	onClosed: (webSocket: okhttp3.WebSocket, code: number, reason: string): void => {
		console.log("WebSocket closed: " + code + ", reason: " + reason);
	},
	onFailure: (webSocket: okhttp3.WebSocket, t: java.lang.Throwable, response: okhttp3.Response): void => {
		console.log("WebSocket failed: " + t.getMessage());
	}
});

Is there a way to have more debug info to check what is really happening?


#2

When you encounter method overloading, do not declare arguments in the function but you can access them using the arguments keyword.


#3

Same error.
I first removed the arguments in onMessage, with no avail, so I removed the arguments from all the methods.
Is there a way to have more log about this kind of error?

interface IWebSocketListener {
	onClosed(): void
	onClosing(): void
	onFailure(): void
	onOpen(): void
	onMessage(): void
}

class WebSocketListener implements IWebSocketListener {
	constructor(implementation: IWebSocketListener)

	onClosed(): void
	onClosing(): void
	onFailure(): void
	onOpen(): void
	onMessage(): void
}
const l = new okhttp3.WebSocketListener({
	onOpen: (): void => {},
	onMessage: (): void => {},
	onClosing:  (): void => {},
	onClosed: (): void => {},
	onFailure: (): void => {}
});

#4

Am also getting same error but mine is index1 when I try to save an image/file to SD fsModule.path.join(imageFolder.toString(), theImage) even if I use the imagesource makes no difference since the error above halts the imagesource code but app still runs


#5

@gdurand could you give us a reproducible case/repository? Maybe we could help you debug the problem and see if we can come up with a workaround/solution.


#6

Well, it appears this WebSocketListener is not an interface but an abstract class… It is so unusual that I didn’t think about checking that.
Sorry for the inconvenience.