wendux / DSBridge-Android

:earth_americas: A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
3.73k stars 613 forks source link

Not find method "sendInvitation" implementation! please check if the signature or namespace of the method is right #146

Closed zhangyubao closed 4 years ago

zhangyubao commented 4 years ago

class PVPMatchGameView : FrameLayout {

private var mContext: Context? = null
private var mWebView: DWebView? = null
private var isBack = "1"

constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
    mContext = context
    initView()
}

private fun initView() {
    val mRootView = LayoutInflater.from(mContext).inflate(R.layout.view_pvp_match_game, this)
    mWebView = mRootView.findViewById(R.id.wv_pvp_webview)
    mWebView?.webViewClient = object : WebViewClient() {

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            mWebView?.settings?.blockNetworkImage = false
        }

        override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
            handler?.proceed()
        }

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            mWebView?.loadUrl(url)
            return true
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
            mWebView?.settings?.blockNetworkImage = true
        }

        override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? {

            if (url.contains("dsbridge.js")) {

                var inputStream: InputStream? = null
                try {
                    inputStream = view.context.assets.open("dsbridge.js")
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                return WebResourceResponse("application/x-javascript", "utf-8", inputStream)
            }

            return super.shouldInterceptRequest(view, url)
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {

            if (request.url.toString().contains("dsbridge.js")) {
                var inputStream: InputStream? = null
                try {
                    inputStream = view.context.assets.open("dsbridge.js")
                } catch (e: IOException) {
                    e.printStackTrace()
                }

                return WebResourceResponse("application/x-javascript", "utf-8", inputStream)
            }
            return super.shouldInterceptRequest(view, request)
        }

    }

}

 fun loadGame(game: PVPGame?, channel: GroupChannel?,activity: Activity) {
    if (channel != null) GplGameActivity.channelUrl = channel
    val base = BuildConfig.GPL_PVP_URL
    var url: String? = "file:///android_asset/gpl_native.html"
    if (game != null && channel !=null) {
        url = "${base}access_token=${ApiUtils.getConfig(Constant.KEY_ENTER_GPL_GAME)}&pvpId=${game.id}&orderId=free&property=${BuildConfig.OAUTH_SOURCE}&gameUrl=${URLEncoder.encode(game.pvpUrl, "utf-8")}&friendType=1${if (BuildConfig.DEBUG) "&vconsole=1" else ""}"
    }
    Logger.e("PVPV 开始加载游戏")
    DWebView.setWebContentsDebuggingEnabled(true)
    mWebView?.addJavascriptObject(this, null)
    mWebView?.loadUrl(url)
}

@JavascriptInterface
fun sendInvitation(friendData: String){
    RxBus.post(EventSendIM(friendData))
}

}
zhangyubao commented 4 years ago

Resolve it. @JavascriptInterface fun sendInvitation(friendData: Any){ RxBus.post(EventSendIM(friendData)) }

函数中定义的方法一定要是Object类型

xixilala-2019 commented 4 years ago

1