|
1 |
| -import 'dart:io'; |
2 |
| - |
3 |
| -import 'package:http/http.dart' as http; |
4 |
| - |
5 |
| -class NetworkException implements Exception { |
6 |
| - final message; |
7 |
| - |
8 |
| - NetworkException([this.message]); |
9 |
| - |
10 |
| - String toString() { |
11 |
| - if (message == null) return "Network Exception"; |
12 |
| - return "Network Exception: $message"; |
13 |
| - } |
14 |
| -} |
15 |
| - |
16 |
| -class Request { |
17 |
| - static http.Client client = http.Client(); |
18 |
| - |
19 |
| - static Future<http.Response> httpGet(String url, Map<String, dynamic> body) async { |
20 |
| - if (body == null) { |
21 |
| - return await client.get(url); |
22 |
| - } else { |
23 |
| - var paramString = '?'; |
24 |
| - body.forEach((k, v) { |
25 |
| - if (v != null) { |
26 |
| - paramString += k + '=' + v.toString() + '&'; |
27 |
| - } |
28 |
| - }); |
29 |
| - try { |
30 |
| - var result = await client.get( |
31 |
| - url + paramString.substring(0, paramString.length - 1), |
32 |
| - ); |
33 |
| - return result; |
34 |
| - } catch (_) { |
35 |
| - throw NetworkException(); |
36 |
| - } |
37 |
| - } |
38 |
| - } |
39 |
| - |
40 |
| - static Future<http.Response> httpPost(String url, Map<String, dynamic> body) async { |
41 |
| - if (body == null) { |
42 |
| - try { |
43 |
| - var result = await client.post(url); |
44 |
| - return result; |
45 |
| - } catch (_) { |
46 |
| - throw NetworkException(); |
47 |
| - } |
48 |
| - } else { |
49 |
| - Map<String, dynamic> paramMap = {}; |
50 |
| - body.forEach((k, v) { |
51 |
| - if (v != null) { |
52 |
| - paramMap[k] = v.toString(); |
53 |
| - } |
54 |
| - }); |
55 |
| - try { |
56 |
| - var result = await client.post(url, body: paramMap); |
57 |
| - return result; |
58 |
| - } catch (_) { |
59 |
| - throw NetworkException(); |
60 |
| - } |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - static Future<http.Response> httpUpload(String url, Map<String, dynamic> body, File file) async { |
65 |
| - try { |
66 |
| - var request = new http.MultipartRequest("POST", Uri.parse(url)); |
67 |
| - if (body != null) { |
68 |
| - body.forEach((k, v) { |
69 |
| - if (v != null) { |
70 |
| - request.fields[k] = v.toString(); |
71 |
| - } |
72 |
| - }); |
73 |
| - } |
74 |
| - request.files.add(await http.MultipartFile.fromPath('file', file.path)); |
75 |
| - var response = await request.send(); |
76 |
| - return http.Response.fromStream(response); |
77 |
| - } catch (_) { |
78 |
| - throw NetworkException(); |
79 |
| - } |
80 |
| - } |
81 |
| -} |
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | + |
| 5 | +class NetworkException implements Exception { |
| 6 | + final message; |
| 7 | + |
| 8 | + NetworkException([this.message]); |
| 9 | + |
| 10 | + String toString() { |
| 11 | + if (message == null) return "Network Exception"; |
| 12 | + return "Network Exception: $message"; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class Request { |
| 17 | + static http.Client client = http.Client(); |
| 18 | + |
| 19 | + static Future<http.Response> httpGet(String url, Map<String, dynamic> body) async { |
| 20 | + if (body == null) { |
| 21 | + return await client.get(Uri.parse(url)); |
| 22 | + } else { |
| 23 | + var paramString = '?'; |
| 24 | + body.forEach((k, v) { |
| 25 | + if (v != null) { |
| 26 | + paramString += k + '=' + v.toString() + '&'; |
| 27 | + } |
| 28 | + }); |
| 29 | + try { |
| 30 | + var result = await client.get( |
| 31 | + Uri.parse(url + paramString.substring(0, paramString.length - 1)), |
| 32 | + ); |
| 33 | + return result; |
| 34 | + } catch (_) { |
| 35 | + throw NetworkException(); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static Future<http.Response> httpPost(String url, Map<String, dynamic> body) async { |
| 41 | + if (body == null) { |
| 42 | + try { |
| 43 | + var result = await client.post(Uri.parse(url)); |
| 44 | + return result; |
| 45 | + } catch (_) { |
| 46 | + throw NetworkException(); |
| 47 | + } |
| 48 | + } else { |
| 49 | + Map<String, dynamic> paramMap = {}; |
| 50 | + body.forEach((k, v) { |
| 51 | + if (v != null) { |
| 52 | + paramMap[k] = v.toString(); |
| 53 | + } |
| 54 | + }); |
| 55 | + try { |
| 56 | + var result = await client.post(Uri.parse(url), body: paramMap); |
| 57 | + return result; |
| 58 | + } catch (_) { |
| 59 | + throw NetworkException(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static Future<http.Response> httpUpload(String url, Map<String, dynamic> body, File file) async { |
| 65 | + try { |
| 66 | + var request = new http.MultipartRequest("POST", Uri.parse(url)); |
| 67 | + if (body != null) { |
| 68 | + body.forEach((k, v) { |
| 69 | + if (v != null) { |
| 70 | + request.fields[k] = v.toString(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + request.files.add(await http.MultipartFile.fromPath('file', file.path)); |
| 75 | + var response = await request.send(); |
| 76 | + return http.Response.fromStream(response); |
| 77 | + } catch (_) { |
| 78 | + throw NetworkException(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments