flutter-io/http

作者: iPhone | 来源:发表于2019-07-03 10:57 被阅读0次
    import 'package:flutter/material.dart';
    import 'dart:io';
    import 'dart:convert';
    import 'package:http/http.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.max,
    //          verticalDirection: VerticalDirection.up,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                      child: OutlineButton(
                        onPressed: dartIo_get,
                        child: Text('io'),
                      ),
                    ),
                  ],
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                      child: OutlineButton(
                        onPressed: http_get,
                        child: Text('http_get'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      }
      
      void dartIo_get() async{
        var url = 'https://httpbin.org/ip';
        var httpClient = new HttpClient();
    
        String result;
        try {
          var request = await httpClient.getUrl(Uri.parse(url));
          var response = await request.close();
          if (response.statusCode == HttpStatus.OK) {
            var json = await response.transform(utf8.decoder).join();
            var data = jsonDecode(json);
            result = data['origin'];
          } else {
            result =
            'Error getting IP address:\nHttp status ${response.statusCode}';
          }
        } catch (exception) {
          result = 'Failed getting IP address';
        }
    
        print('dddddddddddddddd---------$result');
      }
    
      void http_get(){
          get('https://www.jianshu.com/p/782fde5088c0')
              .then((res) {
                print('mmmmmmm --- ${res.statusCode}');
                print('mmmmmmm --- ${res.body}');
            }
          );
      }
    
      void http_post(){
        post('', body: {'password':'e10adc3949ba59abbe56e057f20f883e', 'mobile':'13333333333'})
            .then((response) {
          print("post方式->status: ${response.statusCode}");
          print("post方式->body: ${response.body}");
         }
        );
      }
    }
    

    相关文章

      网友评论

        本文标题:flutter-io/http

        本文链接:https://www.haomeiwen.com/subject/xgmthctx.html