Flutter之网络请求

第一种:原生httpClient方式。
post方式一直不知道怎么用。。。
get方式:

abstract class RequestCallBack {
  void onSuccess(String result);

  void onError();
}

void doRequest(String url, RequestCallBack requestCallBack) async {
    var httpClient = new HttpClient();
    var request = await httpClient.getUrl(Uri.parse(url));
    var response = await request.close();

    if (response.statusCode == 200) {
      var result = await response.transform(utf8.decoder).join();
      requestCallBack.onSuccess(result);
    } else {
      requestCallBack.onError();
    }
  }

第二种:http库方式
在pubspec.yaml中jar
dependencies:
     http: ^0.11.3+16

导入包import 'package:http/http.dart' as http;

代码中创建:
abstract class RequestCallBack {
  void onSuccess(String result);

  void onError();
}

void doRequestPost(String url, Map<String, dynamic>map,
      RequestCallBack requestCallBack) async {
    var client = http.Client();

    client
        .post(
        url,
        body: map)
        .then(
            (response) {
          requestCallBack.onSuccess(response.body);
        })
        .catchError(
            (error) {
          requestCallBack.onError();
        })
        .whenComplete(client.close,);
  }

void doRequestGet(String url,
      RequestCallBack requestCallBack) async {
    var client = http.Client();

    client
        .get(url)
        .then(
            (response) {
          requestCallBack.onSuccess(response.body);
        })
        .catchError(
            (error) {
          requestCallBack.onError();
        })
        .whenComplete(client.close,);
  }


第二种:dio库方式
在pubspec.yaml中jar
dependencies:
    dio: ^1.0.13

导入包import 'package:dio/dio.dart';

abstract class RequestCallBack {
  void onSuccess(String result);

  void onError();
}

void doRequestPost(String url, Map<String, dynamic>params,
      RequestCallBack requestCallBack) async {

    FormData formData = new FormData.from(params);

//    Options options= new Options(
      // 15s 超时时间
//        connectTimeout:15000,
//        receiveTimeout:15000,
//        baseUrl: "",
        // dio库中默认将请求数据序列化为json,此处可根据后台情况自行修改
//        contentType:  ContentType('application', 'x-www-form-urlencoded',charset: 'utf-8')
//    );
    Dio dio = new Dio();
    Response response  = await dio.post(url,data: formData);
    if (response.statusCode == 200) {
      requestCallBack.onSuccess(response.toString());
    } else {
      requestCallBack.onError();
    }
  }


void doRequestGet(String url,
      RequestCallBack requestCallBack) async {
    Options options= new Options(
      // 15s 超时时间
      connectTimeout:15000,
      receiveTimeout:15000,
      baseUrl: "",
      // dio库中默认将请求数据序列化为json,此处可根据后台情况自行修改
//        contentType:  ContentType('application', 'x-www-form-urlencoded',charset: 'utf-8')
    );
    Dio dio = new Dio(options);
    Response response  = await dio.get(url);
    if (response.statusCode == 200) {
      requestCallBack.onSuccess(response.toString());
    } else {
      requestCallBack.onError();
    }
  }

猜你喜欢

转载自blog.csdn.net/weixin_34248705/article/details/86946955