Flutter - 网页抓取

import 'dart:convert';

import 'package:flutter/material.dart';

import 'demo01.dart';
import 'editview.dart';
import 'login.dart';

import 'dart:io';


class HttpDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "MainTab",
      home: HttpDemoPage(),
    );
  }
}

class HttpDemoPage extends StatefulWidget {
  @override
  State createState() {
    return HttpDemoPageState();
  }
}

class HttpDemoPageState extends State {
  bool _loading = false;
  String _text = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("网络")),
      body: SingleChildScrollView(
        child: Column(
          children: [
            RaisedButton(
              child: Text("获取数据"),
              onPressed: _loading ? null : () async{
                setState(() {
                  _loading = true;
                  _text = "请求数据中";
                });

                try{

                  HttpClient httpClient = new HttpClient();
                  HttpClientRequest request=await httpClient.getUrl(Uri.parse("https://www.imooc.com/"));
                  request.headers.add("user-agent", "");
                  HttpClientResponse response=await request.close();
                  _text=await response.transform(utf8.decoder).join();
                  print(response.headers);
                  httpClient.close();


                }catch(e){
                  _text = "请求失败:$e";
                }finally{
                  setState(() {
                    _loading = false;
                  });
                }
              },
            ),
            Container(
              width: MediaQuery.of(context).size.width - 50.0,
              child: Text(_text.replaceAll(new RegExp(r"\s"), "")),
            )
          ],
        ),
      ),

    );
  }
}

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

class HttpJson extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: MyHomeHttpJson()
    );
  }

}

class MyHomeHttpJson extends StatefulWidget{
  @override
  State createState() {
    return MyHomeHttpJsonState();
  }
}

class MyHomeHttpJsonState  extends State{
  var _ipAddress = 'Unknown';


  @override
  Widget build(BuildContext context) {
    var spacer = new SizedBox(height: 32.0);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment:  MainAxisAlignment.center,
          children: [
            new Text('Your current IP address is:'),
            new Text('$_ipAddress.'),
            spacer,
            new RaisedButton(
              onPressed: _getIPAddress,
              child: new Text('Get IP address'),
            ),
          ],
        ),
      ),
    );
  }


  void _getIPAddress() async {
    String result;
    var url = "https://api.apiopen.top/recommendPoetry";
    var httpClient = new HttpClient();
    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['result'];
    } else {
      result =
      'Error getting IP address:\nHttp status ${response.statusCode}';
    }

    setState(() {
      _ipAddress = result;
    });
  }
}

 

你可能感兴趣的:(android)