我在cocos creator上写的http传输脚本,无论是在浏览器上还是模拟器上都没有任何问题, 但一打包到安卓上就连不上网,服务端没有任何消息说明请求根本没发过去,底下是我写的脚本,求助各位大佬能否帮我看一下哪里出了问题
module.exports={
sendPostForms:function(urlApi,paramJson,callback){
//URL未设置
var xhr=new XMLHttpRequest();
this.responseCallback(xhr,callback);
xhr.timeout=5000;
xhr.open("POST","http://106.52.82.57:8000"+"/"+urlApi);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var args='';
for(var i=0;i<paramJson.length;i++){
cc.log(paramJson[i]);
args+=paramJson[i].key+"="+paramJson[i].value+"&";
}
xhr.send(args);
},
responseCallback:function(xhr,callback){
var alert=true;
xhr.onreadystatechange=function(){
console.log(xhr.statusText);
if(xhr.readyState==4&&(xhr.status>=200&&xhr.status<=207)){
alert=false;
var httpStatus=xhr.statusText;
var response=xhr.responseText;
callback(response);
}
}
setTimeout(function(){
if(alert){
callback(JSON.stringify({success:false,res:"错误:网络异常"}));
}
},5000);
},
}
抓包看下,你的手机本身能不能访问106.52.82.57:8000
还有就是你的app有没有联网的权限。
如果服务器也是你写的,在你的服务器上搞一个网页,通过你手机浏览器访问看看。多半还是网络、权限的问题。
虽然不知道你用什么写的,但在标准的android里面是不允许在主线程访问网络的