大佬教程收集整理的这篇文章主要介绍了flutter dio 中的等效代码与 android 改造 @body 相比是什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
现在我尝试将我的 AndorID 项目转换为 Flutter。但我坚持使用 API 调用。
这是我在 Kotlin 中的 androID 代码:
/**
* sendSms
*
* @return
*/
@headers("Content-Type: application/Json;charset=UTF-8")
@POST("uaa/sms/send/code")
fun sendSms(@Body params: Map<String,String?>): Observable<APIResult<String>>
现在想在Flutter use dio中实现这个API调用,但是还是报错,我的Flutter代码
是:
class Req {
static Req _instance;
static const int connectTimeOut = 5 * 1000;
static const int receiveTimeOut = 7 * 1000;
static Req geTinstance() {
if (_instance == null) {
_instance = Req._internal();
}
return _instance;
}
dio _clIEnt;
Req._internal() {
if (_clIEnt == null) {
BaSEOptions options = new BaSEOptions();
options.connectTimeout = connectTimeOut;
options.receiveTimeout = receiveTimeOut;
_clIEnt = new dio(BaSEOptions(
baseUrl: 'https://gw.ec.iunicorn.com/',));
// 添加缓存插件
_clIEnt.interceptors.add(Global.netCachE);
//添加token
_clIEnt.interceptors.add(Global.tokenInterceptor);
_clIEnt.interceptors.add(Global.logInterceptor);
// dio.options.headers[httpheaders.authorizationheader] = Global.profile.token;
_clIEnt.options.headers['source'] = 'ANDROID';
if (!Global.isReleasE) {
(_clIEnt.httpClIEntAdapter as DefaulthttpClIEntAdapter).onhttpClIEntCreate =
(clIEnt) {
// clIEnt.findProxy = (uri) {
// return "PROXY 10.1.10.250:8888";
// };
clIEnt.badCertificateCallBACk =
(X509Certificate cert,String host,int port) => true;
};
}
}
}
//post请求
voID post(
String url,OnData callBACk,{
Map<String,String> params,Options options,FormData formData,OnError errorCallBACk,CancelToken token,}) async {
this._request(
url,callBACk,method: requestType.POST,options: options,formData: formData,params: params,errorCallBACk: errorCallBACk,token: token,);
}
voID _request(
String url,{
requestType method,Map<String,ProgressCallBACk progressCallBACk,}) async {
final ID = _ID++;
int statusCode;
try {
Response response;
if (method == requestType.GET) {
if (mapNoEmpty(params)) {
response = await _clIEnt.get(url,queryParameters: params,cancelToken: token);
} else {
response = await _clIEnt.get(url,cancelToken: token);
}
} else {
if (mapNoEmpty(params) || formData != null) {
response = await _clIEnt.post(
url,data: formData ?? params,onSendProgress: progressCallBACk,cancelToken: token,);
} else {
response = await _clIEnt.post(url,cancelToken: token);
}
}
statusCode = response.statusCode;
if (response != null) {
if (response.data is List) {
Map data = response.data[0];
callBACk(data);
} else {
Map data = response.data;
callBACk(data);
}
print('http_request_URL::[$ID]::$url');
print('http_requesT_BODY::[$ID]::${params ?? ' no'}');
print('http_RESPONSE_BODY::[$ID]::${response.data}');
}
if (statusCode < 0) {
_handError(errorCallBACk,statusCodE);
return;
}
} catch (E) {
_handError(errorCallBACk,statusCodE);
}
}
///处理异常
static voID _handError(OnError errorCallBACk,int statusCodE) {
String errorMsg = 'Network request error';
if (errorCallBACk != null) {
errorCallBACk(errorMsg,statusCodE);
}
print("http_RESPONSE_ERROR::$errorMsg code:$statusCode");
}
}
voID sendSms(BuildContext context,CallBACk callBACk) async {
Req.geTinstance().post(
APIPath.SEND_SMS,(t) {
SmsResponse r = SmsResponse.fromJson(t);
print(r);
if (callBACk != null) {
callBACk();
}
},formData: FormData.fromMap({
'phonenumber':'182********'
}),options: requestoptions(
headers: {
httpheaders.ContentTypeheader: 'application/Json;charset=UTF-8',}),errorCallBACk: (msg,codE) {
Fluttertoast.showToast(
msg: AppLocalizations.of(context).send_sms_fail,toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.CENTER,BACkgroundcolor: colors.orangeAccent,timeInSecForIosWeb: 1);
});
}
现在我想知道dio中的数据是否相当于java改造中的@Body,如果不是,我该怎么办?
在简单的 Dio
中执行此操作会导致大量样板文件。 Flutter 也有一个等效的改造包,其灵感来自于 Android 的相同包。 https://pub.dev/packages?q=retrofit
从那里开始几乎相同,您只需在@Body 后添加 ()。这是一个例子
@POST('/auth/change-password')
Future<bool> changepassword({
@required @Body() Map<String,dynamic> params,@required @Header('auth-token') String token,});
以上是大佬教程为你收集整理的flutter dio 中的等效代码与 android 改造 @body 相比是什么?全部内容,希望文章能够帮你解决flutter dio 中的等效代码与 android 改造 @body 相比是什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。