Skip to content
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

Send data is a JSON string to js while happen error #6

Open
carl1990 opened this issue Jul 6, 2015 · 4 comments
Open

Send data is a JSON string to js while happen error #6

carl1990 opened this issue Jul 6, 2015 · 4 comments
Labels

Comments

@carl1990
Copy link

carl1990 commented Jul 6, 2015

when I call the Method

webView.callHandler(jsHandlerName, data, new CallBackFunction() {
@OverRide
public void onCallBack(String dataFromJS) { } });

and data is a json String as :

{"misp_rate": 0.0, "miss_rate": 0.0, "reading_difficulty": 3, "sentence": "What's your name?", "sentscore": 7.729825044290912, "hypsent": "", "redu_rate": 100.0, "score": 0.0, "frame_stats": [{"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "WHAT'S", "class": "missing"}, {"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "YOUR", "class": "missing"}, {"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "NAME", "class": "missing"}], "result": [{"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "WHAT'S", "class": "missing"}, {"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "YOUR", "class": "missing"}, {"end": 0.0, "fluency": "ok", "start": 0.0, "score": 0.0, "word": "NAME", "class": "missing"}], "overall_score_grade": [1.0, 0.16716503179825531, 0.04837733590220021, 0.005763443891519606, 0.0009540585770522334, 6.666328219118981e-20, 0.0, 0.0, 0.0], "frames": 0.0, "vocal_score": 47.2583396248}

will happen a err:07-06 11:22:17.740: I/chromium(12174): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source: (1)

@SZn007
Copy link

SZn007 commented Dec 21, 2015

i also came across this problem

@mgttt
Copy link

mgttt commented Jun 9, 2016

JSON.stringify() 兼容性有些问题。试试换

function o2s(object){ if(null==object)return "null"; var type = typeof object; if('object'== type){ if (Array == object.constructor) type = 'array'; else if (RegExp == object.constructor) type = 'regexp'; else type = 'object'; } switch(type){ case 'undefined': case 'unknown': return; break; case 'function': case 'boolean': case 'regexp': return object.toString(); break; case 'number': return isFinite(object) ? object.toString() : 'null'; break; case 'string': return '"' + object.replace(/(\\|\")/g,"\\$1").replace(/\n|\r|\t/g, function(){ var a = arguments[0]; return (a == '\n') ? '\\n': (a == '\r') ? '\\r': (a == '\t') ? '\\t': "" }) + '"'; break; case 'object': var pp="";var value =""; var results = []; try{ for (var property in object) { pp=object[property]; value = o2s(pp); if (value !== undefined) results.push('"'+property + '":' + value); }; } catch(e){ } return '{' + results.join(',') + '}'; break; case 'array': var results = []; if(object.length>=0){ for(var i = 0; i < object.length; i++){ var value = o2s(object[i]); if (value !== undefined) results.push(value); }; return '[' + results.join(',') + ']'; } else{ for(k in object) { var kk=k; var value = o2s(object[k]); if (value !== undefined) results.push('"'+kk+'":'+value); } return '{' + results.join(',') + '}'; } break; } }

@carl1990
Copy link
Author

carl1990 commented Jul 5, 2016

这个后来我是在 iframe.src 做了一次编码解决的,其中有些字符串没有做编码导致后边转码会失败的

function _fetchQueue() {
    console.log("++++++++++sendMessageQueue:"+sendMessageQueue);
        var messageQueueString = JSON.stringify(sendMessageQueue);
        console.log("++++++++++messageQueueString:"+messageQueueString);
        sendMessageQueue = [];
        //add by hq
        if (isIphone()) {
            return messageQueueString;
            //android can't read directly the return data, so we can reload iframe src to communicate with java
        } else if (isAndroid()) {
            var url = CUSTOM_PROTOCOL_SCHEME + '://return/_fetchQueue/' + messageQueueString;
            console.log("++++++++++url:"+url);
            messagingIframe.src = encodeURI(url); //修改的地方
        }
    }

@markzhai markzhai added the bug label Jul 5, 2016
@guojunustb
Copy link

我也遇到了同样的问题,这个bug有什么解决方案吗? @markzhai

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

No branches or pull requests

5 participants