Javascript 自定义搜索引擎无法在android手机上运行

Javascript 自定义搜索引擎无法在android手机上运行,javascript,android,html,google-custom-search,Javascript,Android,Html,Google Custom Search,我在浏览器中运行谷歌自定义搜索,效果很好。即使在Phonegap的emulator中也可以正常运行,但在android手机上运行时会出现网络错误或显示空白屏幕。或给出此错误“file:///google.com/cse?q=.... 找不到。” 我使用的是谷歌自定义搜索v2代码。自定义搜索v2的代码如下所示: <script> (function() { var cx = 'ENGINE_ID'; var gcse = document.createElement

我在浏览器中运行谷歌自定义搜索,效果很好。即使在Phonegap的emulator中也可以正常运行,但在android手机上运行时会出现网络错误或显示空白屏幕。或给出此错误“file:///google.com/cse?q=.... 找不到。”


我使用的是谷歌自定义搜索v2代码。

自定义搜索v2的代码如下所示:

<script>
  (function() {
    var cx = 'ENGINE_ID';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>
<script type="text/javascript" src="http://www.google.com/cse/cse.js?cx=ENGINE_ID">

(功能(){
var cx='引擎ID';
var gcse=document.createElement('script');
gcse.type='text/javascript';
gcse.async=true;
gcse.src=(document.location.protocol=='https:'?'https:':'http:')+
'//www.google.com/cse/cse.js?cx='+cx;
var s=document.getElementsByTagName('script')[0];
s、 parentNode.insertBefore(gcse,s);
})();
它大致做的是创建如下标记:

<script>
  (function() {
    var cx = 'ENGINE_ID';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>
<script type="text/javascript" src="http://www.google.com/cse/cse.js?cx=ENGINE_ID">


并将其插入页面上的其他脚本之前。因此,您可以完全跳过这段代码,手动将上面的标记(使用您的引擎id)插入HTML,并且只将
插入HTML。如果仍然无法从file://连接到http://,请下载此脚本(),并将其作为本地javascript文件提供。

我遇到了相同的问题。JavaScript中的一行更改使文件://错误消失:

// This line must take place before initializing GCSE
window.IS_GOOGLE_AFS_IFRAME_ = true;

已在iOS和Android Cordova环境中验证。

您正在使用哪种浏览器?我正在运行phonegap。。它应该在所有浏览器上运行。。。这是phonegap的问题吗?这是因为当与phonegap一起使用时,源的位置是一个文件。因此,文件是协议,但需要http才能访问服务。那么解决方案可能是什么?尝试过了,它给出了错误“无法调用文件中null的“execute”方法”filename.html?q=query“.My js代码是var parameters=location.search.substring(1.split)(&”);var temp=参数[0]。拆分(“”);var query=unescape(temp[1]);var myCallback=function(){google.search.cse.element.render({div:'test',tag:'searchresults-only'});google.search.cse.element.getElement('q')。执行(查询);};window.\uu gcse={parsetags:'explicit',callback:myCallback};my html是Java中的一个替代修复程序:
@Override
public void onReceivedError(final int errorCode,final String description,final String failingUrl){return;}