cocos creator中开发网络通信遇到问题

        var name=(this.parent.getChildByName("nameeditbox")).getComponent(cc.EditBox);
        var strname=name.string;
        var pwd=(this.parent.getChildByName("passwordseditbox")).getComponent(cc.EditBox);
        var strpwd=pwd.string;
        
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var response = xhr.responseText;
                if(response=='-1'){
                    alert("用户名不存在!");
                }
                else if(response=='-2'){
                    alert("用户名或密码不正确!");
                }
                else{
                    cc.sys.localStorage.setItem('daxiong_username',strname);
                    cc.sys.localStorage.setItem('daxiong_password',strpwd);
                    cc.director.loadScene("HomePage");  
                }
            }
        };
        

        xhr.open("GET", config.URL+'/user.ashx?username='+strname+'&password='+strpwd+'&type=1002', true);
        xhr.send();

代码如上。在creator中浏览器浏览,一切正常,但是打包成apk,安装到手机上,http请求就无法发送出去,这是什么原因呢?难道原生的不支持http请求?

我为了排除其他影响因素,将代码简化了一下,试了试,仍然没反应。在cocoscreator 的模拟器里测试,也是正常的,发布原生工程之后,用xcode打开,模拟器运行也不正常。发布apk安装到手机,也不征程。
现在的代码如下:

var strpwd="123123";
            var strname="123123";
            var xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    //alert(10);
                    if(response=='-1'){
                        //alert("用户名不存在!");
                    }
                    else if(response=='-2'){
                        //alert("用户名或密码不正确!");
                    }
                    else{
                        // cc.sys.localStorage.setItem('daxiong_username',strname);
                        // cc.sys.localStorage.setItem('daxiong_password',strpwd);
                        cc.director.loadScene("HomePage");  
                    }
                }
            };
            
            xhr.open("GET", config.URL+'/user.ashx?username='+strname+'&password='+strpwd+'&type=1002', true);

            xhr.send();

各位大神帮忙解决一下,感觉搞不定的话,要放弃creator了。文档太少了,遇到问题就卡住半天。

1)会不会APK包没有网络权限?
2)检查手机防火墙,是不是限制了上网?

不正常的表现是什么?你是说 Mac 模拟器可以发送请求,但是发布原生版本之后,就不正常了?发布的原生版本是什么平台?Mac ?用的是什么模版?jsb-default ?

另外, config.URL 是什么?如果是 http,苹果对 http 请求有限制

首先多谢您的答复,作为小白用户,不胜感激!
我在把问题描述一下:
点击登录按钮,本应向服务器发起一次http请求(get方式),在cocos creator里使用浏览器和模拟器,点击按钮后都能正常访问到服务器,并且拿到服务器的response。但是,我用jsb-default里生成的Proj.ios_mac项目,用xcode打开工程,调用苹果模拟器调试,发现点击登录按钮后,服务器端没有收到请求,我在服务器端加了日志发现的,这次请求并没有发起,服务器端什么都没有收到。我的URL是http://47.92.106.252/
完整的url地址是:http://47.92.106.252/user.ashx?username=123123&password=123123&type=1001

另外,我打包出来的Publish文件夹下的apk,安装到手机上之后,也是点击登录按钮,没有相应,跟xcode里一样。但我用手机浏览器直接访问url完整地址,是可以拿到返回值的。

网络权限应该是有了:

手机应该也可以上网。我用手机浏览器直接访问服务器的url,是可以拿回来返回值的。

再加个
xhr.onerror
可能是出错了

还有就是onreadystate里面再加个else,可能是状态不在你的范围内

iOS 默认会通过 App Transport Security 阻止一切 http 请求,你需要用白名单机制,或者是关闭 App Transport Security。

https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9

Android 要看一下 log,用 Android Studio 调试一下 JAVA 层的 org/cocos2dx/lib/Cocos2dxHttpURLConnection.java 里面的逻辑,看看是哪里出了问题

ios的App Transport Security问题,我这边一开始就关闭了。
不知是否可以把我的代码发给您,您帮忙看一看,很简单的几个scene,我们现在在立项,选择开发工具,卡在这里一天了,不搞定这个问题,不是很敢用这个工具。

我现在把多余的代码全都去掉了,直接用官方的例子拷贝过来,结果也是在creator里浏览器和模拟器里看都没问题,但是发布成apk,安装到手机后,点击按钮无反应。。真是醉了。
var name=(this.parent.getChildByName(“nameeditbox”)).getComponent(cc.EditBox);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
var response = xhr.responseText;
console.log(response);
name.string=response;
}
};
xhr.open(“GET”, “http://ysdzz.zhongbotech.com/user.ashx?type=1001&username=123123&password=123123”, true);
xhr.send();

你把项目发来吧,我看看

好好,多谢多谢,请问怎么发您啊。我的qq是724767213

目测应该是你的问题

解决了,原因是用 mousedown 去触发发送请求的事件,居然一开始没注意到 :weary:
改成监听 touchend 就没问题了

你好,我想问下,我用的CocosCreator1.4.2版本的,我的打包在android上经常会遇到这个问题,特别是网络不是很好的情况下(这个时候打开网页,打开其他app都没问题,说明还是有网的),经常http发请求服务器收不到,这个世引擎的bug吗?

各位同学,我的问题解决了,我做一下统一的回复,给各位做个参考。十分感谢cocos引擎开发团队的panda,非常耐心,非常热心的在帮我解决问题,知道问题完全解决。cocos团队还是十分给力的,我表示以后就是忠实粉了。
问题是这样:
我最初发的那段代码一共前后解决了三个问题,
问题一,我使用了alert这个js中常用的弹框,但是这个在手机端是不支持的,只要有这个代码,手机端会直接报错的,后来改成console.log就可以了。
问题二,ios上发送http请求是默认不支持的,而只支持https。这个在panda的帮助下,修改了指定位置,就好了,大家可以参考panda 的回复。
问题三,也是我最傻的一个错误,我代码上大家仔细看,我绑定的是mousedown事件,当时也没太在意,反而是这个最简单的地方出问题了,手机端是没有mousedown这个事件的,因此手机上肯定点击了没有响应,后来改成touchend事件就可以了。
最新的代码如下:经测试apk没问题,ios打包也没有问题,creator模拟器里调试就更没问题了。

this.node.on(**'touchend'**, function ( event ) {
            var name=(this.parent.getChildByName("nameeditbox")).getComponent(cc.EditBox);
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    console.log(response);
                    name.string=response;
                }
                else name.string=xhr.readyState +"|"+xhr.status;
            };
            xhr.onerror=function(){
                 name.string=xhr.readyState +"|"+xhr.status;
                
            }
            //dai ma代码gen代码跟gei ni代码跟给你 de的yi yang的一样 
            xhr.open("GET", "http://xxxxx.xxxxxx.com/user.ashx?type=1001&username=123123&password=123123", true);
            xhr.send();

希望对各位有帮助。
另外,在构建发布的时候,选择armeabi-v7a,可以兼容目前主流的手机,如果选择arm64-v8a,则有可能导致古老的andrdoid手机无法安装apk,我的小米note2遇到了这个问题,改了之后,就能顺利安装了。

你好,你这个问题解决了吗