处理HTTP访问异常

Android社区 收藏文章

An HTTP request may return a response, or it may generate an error.

import 'package:http/http.dart' as http;

handleSuccess(http.Response response) {
  print('something went right!');
  print(response.body);
}

handleFailure(error) {
  print('Something went wrong.');
  print(error.message);
}

main() async {
  try {
    var response = await http.get("http://some_bogus_website.org");
    handleSuccess(response);
  } catch (e) {
    handleFailure(e);
  }
}
相关标签

扫一扫

在手机上阅读