目录
概述
http插件的功能是发送http请求,包括get、head、post请求;http请求相关的控制,如sslpinning、证书、域名;文件的上传和下载,因上传文件需要服务器,本文未提供上传的示例。
JS调用方法
1、导入插件require(“cordova!cordova-plugin-http”);
2、插件提供了8个方法:
- get(url, params, headers),get请求:url代表链接,params代表参数,headers代表http请求头
- head(url, params, headers),head请求,参数与get请求一致。
- post(url, params, headers),post请求,参数与get请求一致。
- enableSSLPinning(enable),允许或禁止sslpinning方法,enable为boolean类型,取值true或者false。
- acceptAllCerts(allow),允许或禁止证书方法,allow为boolean类型,取值true或者false。
- validateDomainName(validate),允许或禁止域名方法,validate为boolean类型,取值true或者false。
- downloadFile(url, params, headers, filePath),文件下载方法,url代表链接,params代表参数,headers代表请求头,filePath代表文件路径。
- uploadFile(url, params, headers, filePath, name),文件上传方法,url代表链接,params代表参数,headers代表请求头,filePath代表文件路径,name代表文件名。
define(function(require){
var $ = require("jquery");
require("cordova!cordova-plugin-http");
var utils = require("$UI/demo/plugin/utils");
var Model = function(){
this.callParent();
};
//成功回调,data.status为请求返回的状态值,data.data为返回的数据
function success(data){
alert("状态值:"+data.status+"\n 返回数据:"+data.data);
}
//失败回调,data.status为请求返回的状态值,data.data为返回的数据
function failure(data){
alert("错误返回状态值:"+data.status+"\n 返回数据:"+data);
}
//下载文件成功返回
function fileSuccess(data){
alert("标题:"+data.headers+"\n 状态值:"+data.status+"\n 返回数据:"+data.file);
}
//上传下载文件失败返回
function fileFailure(data){
alert("\n 状态值:"+data.status+"\n 返回数据:"+data.error);
}
//get请求
Model.prototype.getClick = function(event){
cordovaHTTP.get("http://www.baidu.com", {}, {}, success,failure);
};
//head请求
Model.prototype.headClick = function(event){
cordovaHTTP.head("http://www.baidu.com", {}, {}, success,failure);
};
//post请求
Model.prototype.postClick = function(event){
cordovaHTTP.post("http://www.baidu.com", {}, {}, success,failure);
};
//SSLPinning协议开关
Model.prototype.enableSSLPinningClick = function(event){
cordovaHTTP.enableSSLPinning("true",function success (){alert("允许SSL pinning")},function fail(){alert("禁止SSL pinning")});
};
//证书开关
Model.prototype.acceptAllCertsClick = function(event){
cordovaHTTP.enableSSLPinning("true",function success (){alert("允许证书")},function fail(){alert("禁止证书")});
};
//域名开关
Model.prototype.validateDomainNameClick = function(event){
cordovaHTTP.acceptAllCerts("true",function success (){alert("允许域名")},function fail(){alert("禁止域名")});
};
//文件下载
// 路径"cordova.file.dataDirectory"的含义参考cordova-plugin-file插件的介绍:http://docs.wex5.com/cordova-plugin-file/
Model.prototype.downloadFileClick = function(event){
cordovaHTTP.downloadFile("http://192.168.1.104:8080/1.jpg", {}, {},cordova.file.dataDirectory+"/1.jpg",fileSuccess,fileFailure);
};
//上传文件
Model.prototype.uploadFileClick = function(event){
cordovaHTTP.uploadFile("http://192.168.1.104:8080/", {}, {},cordova.file.dataDirectory+"/1.jpg","picture",success,fileFailure);
};
Model.prototype.modelModelConstructDone = function(event) {
utils.showCode(this,"getClick").showCode("headClick").showCode("postClick").showCode("enableSSLPinningClick").showCode("acceptAllCertsClick").showCode("validateDomainNameClick").showCode("downloadFileClick").showCode("uploadFileClick").prettyPrint();
};
return Model;
});
注意事项
1、上传文件需要服务器接收,本文举例的url用了tomcat的首页,插件执行该方法的时候会返回成功,只代表该url能成功访问,设备上也确实存在该文件。不代表url的地址正确接收了该文件。
2、上传下载的filepath参数,使用了file插件提供的方法。关于filepath的使用方法和对应目录的权限等参考cordova-plugin-file插件的介绍:http://docs.wex5.com/cordova-plugin-file/。
3、需要下载的文件,可以是网上的资源,也可以是本地服务器里的资源,本文例子的资源放到了tomcat服务器目录,apache-tomcat\webapps\ROOT,启动tomcat服务器后放在该目录下的文件可以被访问和下载。
评一波